How can I tell if a connection was obtained within a UserTransaction scope
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Javier_Ramos
Posted On:   Monday, June 30, 2003 02:13 AM

Hello, I have got the following problem: We are running a java web project with Websphere. The database access was made initially using DB2ConnectionPoolDataSource. All the Connection, Statement, PreparedStatement, ResultSet... etc objects are opened and closed via a singleton object called DBConnectionManager. Also the UserTransaction objects are obtained from this singleton object. Eventually we will wrap the objects we need from in java.sql.* and javax.sql.* inside our own classes, so that the use of classes in java.sql.* and javax.sql.* are forbidden in the rest of the application and we can enforce a programming model for    More>>

Hello,


I have got the following problem:


We are running a java web project with Websphere.


The database access was made initially using
DB2ConnectionPoolDataSource.


All the Connection, Statement, PreparedStatement, ResultSet... etc objects are opened and closed via a singleton object called DBConnectionManager.


Also the UserTransaction objects are obtained from this singleton object.


Eventually we will wrap the objects we need from in java.sql.* and javax.sql.* inside our own classes, so that the use of classes in java.sql.* and javax.sql.* are forbidden in the rest of the application and we can enforce a programming model for the database access. For example, not allowing to open or close a connection if it is not through the singleton manager.


The DataSource we were using, by default creates connections with AutoCommit set to TRUE.


Now we want to move to a driver with two phase commit support, as we need to coordinate transactions across different databases.


Unluckily our XADataSource now creates the connections with autoCommit set to FALSE.


This means that in every method updating the database now we need to call commit on the connection before closing it. Otherwise closing the connection rolls back the work done so far.


We thought of doing so through a new method
DBConnectionManager.commit( Connection con ) in the singleton database access manager, where we could ask if con.autoCommit is false or true and issue a commit or not, respectively.


But then this will fail whenever we are working with autoCommit FALSE and the connection was obtained inside a UserTransaction scope, as then it is not valid to issue commit on any connection.


So what I need to know is if there is an easy way to detect that the connection was obtained within a UserTransaction scope, as then I will be able to tell DBConnectionManager.commit( Connection con ) NOT to
commit even though con.autoCommit is FALSE.


If this is not possible, anyone can think of another solution?


As you will understand, my intention is to be able to write methods that access and update data which need not be aware of themselves being participating or not in transactions.


Any help would be greatly appreciated.


Ciao...

   <<Less

Re: How can I tell if a connection was obtained within a UserTransaction scope

Posted By:   jason_jack  
Posted On:   Monday, June 30, 2003 05:22 AM

If you want to stick to your current design of the application you could wrap your Connection with an Object that adds a flag (boolean). Whenever you are getting a connection inside a UserTransaction set the flag to true; otherwise leave it to false.

About | Sitemap | Contact