• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

EJB Transaction Problem

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,

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.
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)

Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic