File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes EJB Transaction Problem 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 "EJB Transaction Problem" Watch "EJB Transaction Problem" New topic

EJB Transaction Problem

K Ediz

Joined: Sep 25, 2009
Posts: 13


I am trying to persist a data by using EntityManager.persist method. I am using EJB 3.0 Stateless Session Facade Bean and its transaction is Container-Managed. (CMP)
After persist operation, within the same ejb method, I am trying to update the data by using a JDBC "Update" query. But the problem is that the data is not updated. The transaction is not committed before the execution of the ejb method finishes, I know that. But even if that's the case, the data should be updated, as I know.

The code is given below:

So when I check the data from the database, the created_by column remains as "H", it is not updated. But when I try to update the data by using the pojo and EntityManager.merge method with the given code below:

The data is updated in the database in the same transaction of persisting.

But I need to use the JDBC query within the ejbMethod.

I have tried "conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);" , but it did not work.

I think the transaction that the jdbc connection uses is different than the transaction that the persist operation uses.

How will I resolve this problem? Is there any setting that I need to configure?

Thanks in advance.
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 195
The update is executed but it cannot find a row inside the database because the entity manager didn't execute the insert statement yet. He does this right before commiting. You could call flush(). But what for? If you insert data, why do you want to update then right then? If you create the entity, call persist and change a field within that entity you do not need to call merge. With calling persist the entity is managed by the entity manager and every change is being persisted when the entity manager inserts and commits. So your second statement does not trigged INSERT and UPDATE, it only triggers (because of inserting just before commiting) an INSERT with createdBy=K. If you call flush() you'll see INSERT and UPDATE.
K Ediz

Joined: Sep 25, 2009
Posts: 13
The code example that I gave was a dummy example.In reality, the update statement tries to update a column which is not a POJO attribute, so I cannot use em.merge or em.persist, I need to use a custom jdbc query.

Anyway, I found the solution:

If I use a new Connection from the datasource, then the transaction is not managed by container and the query will use an independant transaction which cannot be attached to the CMP transaction.

So I changed the code a bit like this:

I am not using the JDBC Connection, I use the EntityManager's createNativeQuery method which means that the transaction that it uses for the update query is the one that is managed by the container. (same transaction for the persist operation)

I agree. Here's the link:
subject: EJB Transaction Problem
It's not a secret anymore!