Hibernate: Transaction not successfully started (Threaded webapp) Problem!
I am having this serious problem in a web-app that is using a thread to listen on a port for packets and process those packets.
Processing of a packet involves database transactions, so basically it is like :
Thread:
run () {
try {
...
fireMessageEvent(data);
...
} catch (Exception e) {
}
}
fireMessageEvent(data) {
someclass.messageArrived(data);
}
SomeClass:
messageArrived(Data data) {
try {
someOtherClass.setData(data);
} catch (Exception e) {
}
}
SomeOtherClass:
setData (Data data)
{
try {
..
processDataPart1(data);
processDataPart2(data);
..
} catch (Exception e) {
}
}
processDataPart1(data)
{
daolayer.getSomeObjectFromDatabase(x);
..
daolayer.save(y);
..
someotherDaolayer.update(a);
..
}
processDataPart2(data)
{
daolayer.getSomeObjectFromDatabase(x);
..
daolayer.save(y);
..
someotherDaolayer.update(a);
..
daolayer.save(b);
..
}
The problem is that I get an exception 'Transaction not sucessfully started'. Also interestingly the exception gets caught by the catch in the thread, and hence doesnot save the data (probably due to rollback.?.). For the moment, I create another dummy object to save in the database and then delete it, so that when the exception occurs its the dummy object that doesn't get saved and the previous objects do get saved.
Can anyone help on this? Has anyone encountered the same problem?
Here is the exception details:
2010-02-25 10:30:33,759 INFO [packetInfo log] - Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started 2010-02-25 10:30:33,759 INFO [packetInfo log] - org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:659) 2010-02-25 10:30:33,759 INFO [packetInfo log] - org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:732) 2010-02-25 10:30:33,759 INFO [packetInfo log] - org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:701) 2010-02-25 10:30:33,759 INFO [packetInfo log] - org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:321) 2010-02-25 10:30:33,759 INFO [packetInfo log] - org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116) 2010-02-25 10:30:33,774 INFO [packetInfo log] - org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) 2010-02-25 10:30:33,774 INFO [packetInfo log] - org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635) 2010-02-25 10:30:33,774 INFO [packetInfo log] - com.comp.projectx.service.lear.communicator.AriadneListenerServiceImplementation$$EnhancerByCGLIB$$e5054dfb.setData() 2010-02-25 10:30:33,774 INFO [packetInfo log] - com.comp.projectx.service.lear.communicator.PLCListenerServiceImplementation.messageArrived(PLCListenerServiceImplementation.java:153) 2010-02-25 10:30:33,774 INFO [packetInfo log] - com.comp.projectx.service.lear.communicator.Listener.fireMessageEvent(Listener.java:229) 2010-02-25 10:30:33,774 INFO [packetInfo log] - com.comp.projectx.service.lear.communicator.Listener.run(Listener.java:94)
Configuration in web.xml:
hibernateFilter : org.springframework.orm.hibernate3.support.OpenSessionInViewFilter hibernateFilter url-pattern: *.95 hibernateFilter url-pattern: /dwr/* lazyLoadingFilter: org.springframework.orm.hibernate3.support.OpenSessionInViewFi开发者_如何学Golter lazyLoadingFilter url-pattern: /*
Hibernate Configuration:
jee:jndi-lookup id="dataSource" jndi-name="/jdbc/kernstreamDS" resource-ref="true" expected-type="javax.sql.DataSource"
bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
property name="dataSource" ref="dataSource"
bean
bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
In the Dao layer to save an object:
getSession().saveOrUpdate(resultObj);
to retrieve:
List resultObj = getSession().createCriteria(Lot.class)
.add(Restrictions.eq("publicId", publicId)).list();
Any help would be greatly appreciated.
I solved the problem. In case anyone else had the same issue here is how I did it.
Basically, in the dao layer, it was using Spring transaction management and at other locations it was using hibernate transaction management. Mixing the two is not a good idea. So by changing in the dao layer from using
getSession().saveOrUpdate(...);
to
getHibernateTemplate().saveOrUpdate(...);
solved the issue.
Thank you all for the comments.
I had similar issue, In my situation there was a problem in the connection, obviously if no connection then no session, if no session no transaction.
try {
session = HibernateSessionFactory.getSessionFactory()
.openSession();
session.beginTransaction();
StringBuffer qry = new StringBuffer();
qry.append(" select a.customer_key from customer as a where a.name= '"+siteName+"' ");
query = session.createSQLQuery(qry.toString());
customerList = (ArrayList) query.list();
session.getTransaction().commit();
} catch (HibernateException hex) {
session.getTransaction().rollback();
String msg = "Error occured in getCustomerId Cause: " + hex;
logger.error(msg);
throw new DAOException(msg);
}finally{
if(session != null){
session.close();
}
}
In the above snippet a rollback occurs to a session that does not even exist. This resulted in Transaction not successfully started. So I added a nested try catch block to avoid this error.
So my modified code looked something like this.
try {
session = HibernateSessionFactory.getSessionFactory()
.openSession();
session.beginTransaction();
StringBuffer qry = new StringBuffer();
qry.append(" select a.customer_key from customer as a where a.name= '"+siteName+"' ");
query = session.createSQLQuery(qry.toString());
customerList = (ArrayList) query.list();
session.getTransaction().commit();
} catch (HibernateException hex) {
try{
session.getTransaction().rollback();
}catch(Exception e){
//just log warn
}
String msg = "Error occured in getCustomerId Cause: " + hex;
logger.error(msg);
throw new DAOException(msg);
}finally{
if(session != null){
session.close();
}
}
精彩评论