aspose file tools*
The moose likes Spring and the fly likes How to commit JDBC prior to JMS in a Transaction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to commit JDBC prior to JMS in a Transaction" Watch "How to commit JDBC prior to JMS in a Transaction" New topic
Author

How to commit JDBC prior to JMS in a Transaction

Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Hi,

I work on two resources (JMS & JDBC). I use WebsphereUowTransactionManager. But JMS transactions are getting committed prior to the JMS transaction. This seems to be the default behavior. Is there a way to have a reverse behavior where JDBC is committed prior to JMS commit..??


Ashwin Sridhar
SCJP | SCWCD | OCA
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Hi guys any suggestions
Rishi Shehrawat
Ranch Hand

Joined: Aug 11, 2010
Posts: 218

Spring supports this type of scenario, basically the scenario where database is committed before JMS is called "best effort strategy" for managing transaction involving JDBC & JMS without using JTA. If you are using the jms listener-container provided by Spring for consumption of messages, then for the acknowledge property you need to specify "transacted". In this case Spring will then synchronize with transaction started by local transaction manager like DataSourceTransactionManager. I am not very sure if it will work with WebsphereUowTransactionManager as it is implementation of JTA transaction manger from IBM. With WebsphereUowTransactionManager you also have a option of using JTA transactions.
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Thanks for the response.

I have a approach, which i need to test it out.

I plan to introduce a separate Transaction Boundary for JDBC within the JMS Transaction Boundary. By this way, i could commit/ rollback JDBC transaction before the JMS commit/rollback.

Do you think this approach would work out positively.
Rishi Shehrawat
Ranch Hand

Joined: Aug 11, 2010
Posts: 218

Your approach should work, as Spring internally will also be doing something like this. However the downside is that you will have to write code for doing all this which can introduce potential bugs, cause maintainence issues & is not scalable if you have a lot of places in your app where JMS & database transactions need to work.

I would try to use Spring to do this for me & will revert back to writing code if Spring is not able to cater to the scenario.
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

I tried various other configuration changes where i could make JDBC commit prior to JMS commit. I use JMSTemplate102 and tried using JTATransactionManager, WebSphereUowTransactionManager but nothin turned out to be helpful.
Rishi Shehrawat
Ranch Hand

Joined: Aug 11, 2010
Posts: 218

I had got it working with jms listener container & DataSourceTransactionManager. Have shared configuration below. I am not very sure if it will work with JTA. In case you are not using XA transactions you can use DataSourceTransactionManager.


Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Hi Rishi,

Thanks for the response. I am using XA transaction along with JDBC. So i guess, in order to override the default approach of JMS-COMMIT-FIRST-FOLLOWED-BY-JDBC-COMMIT, i indeed have to go in for nexted transactions.

The approach i told is working, but it may not be the best approach i guess.
Rishi Shehrawat
Ranch Hand

Joined: Aug 11, 2010
Posts: 218

Do you have 2 different databases that requires XA transactions. Basically a situation where 2 resource managers are being used require XA transactions. In case you are already using transactions then JMS can also participate in JMS transaction. In this case the "best effort strategy" of committing database before JMS is not required. For JTA to work you need to use XA aware resources (drivers for JDBC & connection factories for JMS).
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

I have a single Database and a single Queue. I already use both of them within a transaction. My problem is, JMS gets committed first followed by JDBC. So application listening to queue reacts first and searchs for data in Database and ends up in No Data Found Exception. However , by redelivering the message in the queue, the issue is handled. But it is not what desired.

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Yeah, it sounds more to me that you need two transactions "nested"

You could do REQUIRES_NEW on the jdbc call. And when it comes back, then send the JMS Message within its own transaction.

Hope that helps.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

HI ,

Thats wat exactly i did. But there is a drawback in that approach. If the Jdbc Succeeds and Jms fails, i cannot make the JDBC to rollback.

So i made my Jdbc , a non - XA resource. So automatically JDBC commits prior to JMS.
Guido Laures
Greenhorn

Joined: Feb 06, 2012
Posts: 1
This is a pretty nice way to solve the issue: http://do-on-it.blogspot.com/2012/02/spring-database-transactions-before.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to commit JDBC prior to JMS in a Transaction
 
Similar Threads
Transaction propagation and Persistence Context synchronization with Oracle Stored Procedures
Transaction management
Spring Jms Transaction
2 phase commit
JDBC transactions, JTA transactions, Transaction demarcation