Two Laptop Bag
The moose likes EJB and other Java EE Technologies and the fly likes opening a DB Connection after Usertran.begin() stmt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "opening a DB Connection after Usertran.begin() stmt" Watch "opening a DB Connection after Usertran.begin() stmt" New topic

opening a DB Connection after Usertran.begin() stmt

Karthik Prabu
Ranch Hand

Joined: Dec 27, 2002
Posts: 48
Hi All,

We have a scenario where we have to write Bean Managed Stateless Session EJB. We have noticed that, if we get the DB Connection from Connection pool after usertran.begin(), then if there is any error proper rollback is done after issuing usertran.rollback(). But if we get the connection before usertran.begin() statement, rollback is not happening as expected.

For some reasons we need to have the connection created before the Transaction begins. Is there any workaround for this ? Why the system is behaving like this?

We use
App. Server - WLS 8.1
DB server - Sybase 12.5
JDBC Driver - weblogic.jdbc.sybase.SybaseDriver

KasiMurugan Ramasamy
Ranch Hand

Joined: Jan 30, 2005
Posts: 125

So from your spec, i could understood that you need two types of connection. They are, one should be participate in a transaction and another one shouldn't not be participate in transcation. Am i right?.

For that why you don't go for two types of connection. Before transaction begins and after transaction.begin().Because EJB spec for Transaction says that when you specify transaction.begin() then only the container enlist connection which are managed by container for transaction.

I hope you will get me.

Thanks & Regards
Kasimurugan (SCJP1.4, SCBCD1.3), Preparing SCWCD1.4
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
It is true that it does sometimes matter when you get the connection. I don't know all the circumstances, but I do know that distributed transactions using non-XA drivers must start before the connection is obtained, otherwise it doesn't matter if using an XA driver.

I'd say that you should always obtain your connection after you start a JTA transaction. If you can't, then your design is not good. I suspect that your Connection variable is not local: if so, always make it local or you will lay yourself open to problems.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
I agree. Here's the link:
subject: opening a DB Connection after Usertran.begin() stmt
It's not a secret anymore!