-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Concurrency Problem in Hibernate Application
PostPosted: Wed Jul 02, 2008 6:06 am 
Newbie

Joined: Thu Jun 05, 2008 3:52 am
Posts: 10
I have created hibernate application using EntityManager and Annotations based API. I am adding record in database, so its working fine. But, when i tried to send 5-6 request at same time then i am getting some exception.

My Code is :

public class Transaction {

public boolean createEntity(Object object){

EntityManager entityManager = null;
EntityTransaction transaction = null;

boolean isCreated = false;

try {
System.out.println("Persist Entity...");

entityManager = JPAEntityManagerFactory.getEntityManager();

transaction = entityManager.getTransaction();

transaction.begin();
entityManager.persist(object);
transaction.commit();

isCreated = true;

} catch (Exception e) {

System.out.println("Exception in createEntity : " + e.getMessage());

// if anything go wrong then perform rollback
if (entityManager.getTransaction().isActive())
entityManager.getTransaction().rollback();

isCreated = false;

}finally{

transaction = null;

entityManager.close();
entityManager = null;

System.out.println("Entity Persisted....");
}

return isCreated;

}

}

So, when more than one request is trying to persist object then its giving exception.
"Transaction already active"

I debug the application. When two requests are trying to access same function at same time, and one request reaches at the end of function and another request is in the middle of function then its giving me exception.

So, My query is :

1. In above function if i check transaction.isActive(), If it is not active then do transaction.begin(). Then above error will be resolved. so, is that appropriate way to handle multiple request, all will use same database connection ?
2. Do i need to close entityManager in finally block ?
3. Do i need to explictly write transaction.commit() ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.