views:

40

answers:

2

i am using session bean in my application and transaction is controlled at EJB layer only. The problem that i am facing is with some commit. I am using the same connection as used by EJB to insert into one table, but if the transaction is committed then that insert is not committed into the database.. can any one help me with the problem..

+2  A: 

I'm not an EJB expert, I usually work with plain old java objects, but...

Your problem probably has to do with the fact that EJBs don't do transaction management on the connection level. They use the Java Transaction Service to create transactions that can use multiple connections. So for your insert to become part of the EJB's transaction you have to obtain that transaction from the transaction server and make your insert part of that transaction. I believe that how you do this exactly depends on what kind of EJB environment you have (EJB2 or 3 and so on)

But if your in an EJB environment and want to insert stuff within the same transactions as a EJB does its stuff, would it not make sense to also make an EJB for the table that you want to insert into and let the application server figure it out?

Fried Hoeben
A: 

General, simplified, answer to your question is yes, Connection.commit() is called when EJB is committed.

What EJB actually does depends on how the datasource is defined (transactional or not) and if the last resource optimization is allowed.

I am using the same connection as used by EJB

How do you know? Some connection wrappers (e.g. Weblogic one, if I remember right) have no way to compare two connections for equality. To do that one need to use a vendor API. So even if you think two connections are the same, it's not necessary the case.

How did you get that connection? From where? Depending on EJB version you should only get connection from transactional datasource (EJB2) or use persistence context and JPA (EJB3). Some simplified code of what you do would greatly help to point onto your mistake.

Vladimir Dyuzhev