Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

ThreadLocal to hold Connections good practice in EJB environment?

843829Sep 26 2002 — edited Oct 7 2002
I need your advice regarding common and good practices regarding EJB and JDBC programming.
I write a stateless session bean with bean managed transactions using standard JDBC transactions. So in a method I do following: 1. checkout a database connection from the pooled datasource,
2. do all the needed work in the database (using stored procedures),
3. COMMIT or ROLLBACK the transaction
4. close the database connection to put it back into the pool.

Now the question is whether it is better to pass the connection object to each called class/method, or to use a ThreadLocal singleton to store the Connection object "globally" where it can be retrieved from any method that needs a database connection.
I would use the ConnectionDispenser as described in the article "Threading lightly, Part 3" from Brian Goetz (http://www-106.ibm.com/developerworks/java/library/j- threads3.html).

A further problem is that I want to use SQLJ that retrieves the database connection from a DefaultContext singleton by default. To enable this to work in a EJB environment I have to retrieve a new ConnectionContext from my ConnectionDispenser in each method. If you have better solutions for this, please let me know.

Thanks for your contributions,
Robert
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 4 2002
Added on Sep 26 2002
9 comments
336 views