-->
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.  [ 6 posts ] 
Author Message
 Post subject: 1 Session or 2 Session for 2 task?
PostPosted: Thu Mar 24, 2005 8:55 pm 
Beginner
Beginner

Joined: Thu Jan 13, 2005 10:31 am
Posts: 20
Hibernate version: 2.17


In my code I have to do 2 tasks. The 2nd task I want to do only when the 1st task has been completed successfully. If any exception is thrown in 2nd task (whether I am catching it or not), I still want the 1st task to remain successful. My question is should I use 1 session for both tasks or 1 session for each task? I am using ThreadLocal pattern

I wrote the code this way:

try {
HibernateUtil.getSession();

HibernateUtil.beginTransaction();

// do 1st task
...

HibernateUtil.commitTransaction();

HibernateUtil.beginTransaction();

// do 2nd task
...

HibernateUtil.commitTransaction();

} catch...
finally {
HibernateUtil.closeSession();
}

Is this the right way? I find out that if an exception is thrown in 2nd task then the 1st task usually remains committed (correct). Only one time it gets rollback and I have not been able to reproduce it.
I do sometime get this error message :

[LocalTxConnectionManager$LocalConnectionEventListener] throwable from unregister connection java.lang.IllegalStateException: Trying to return an unknown connection2!
....

Thanks

Uryl


Top
 Profile  
 
 Post subject: Re: 1 Session or 2 Session for 2 task?
PostPosted: Sun Mar 27, 2005 4:26 am 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
uryl wrote:
Is this the right way? I find out that if an exception is thrown in 2nd task then the 1st task usually remains committed (correct). Only one time it gets rollback and I have not been able to reproduce it.
I do sometime get this error message :



I agree that this should be the way to go, but I have read in the docs that when you commit a session, you should start a new one. So that is probably what you should do, especially if you have already found problems with the other (more logical) way. :)

Chris


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 28, 2005 11:25 am 
Beginner
Beginner

Joined: Thu Jan 13, 2005 10:31 am
Posts: 20
ok I did some testing :

Main method
=========
try {
HibernateUtil.getSession();
HibernateUtil.beginTransaction();

// do 1st task
....

HibernateUtil.commitTransaction();

// call 2nd task
String returnMessage = ....

} catch (...) {
...
HibernateUtil.rollbackTransaction();
} finally {
System.out.println("Inside the finally block");
HibernateUtil.closeSession();
}


2nd task method
===========
try {
HibernateUtil.getSession();
HibernateUtil.beginTransaction();
...
// if any business rule fails here then I rollback the transaction and return

HibernateUtil.commitTransaction();
} catch (...) {
...
HibernateUtil.rollbackTransaction();
}


Then in the 2nd task method I put deliberately some code to get it failed at runtime but will not catch the exception
int test = shiftid[3];
I know that it will fail and ArrayIndexOutOfBoundsException will happen. But I am not catching it my 2nd method code.

On running I find out that the moment the above line is reached, Hibernate returns to the Main method; executes the finally block; close the Session and then report a "Trying to return an unknown connection2!" error message before reporting the "ArrayIndexOutOfBoundsException" in log file.

My question is why Hibernate is doing like this?

Thanks

Uryl


2005-03-28 09:52:36,423 INFO [STDOUT] ShiftManager.java: updateSchedule(): get into finally block
2005-03-28 09:52:36,423 INFO [STDOUT] HibernateUtil: Closing Session of this thread.
2005-03-28 09:52:36,423 DEBUG [net.sf.hibernate.impl.SessionImpl] closing session
2005-03-28 09:52:36,423 DEBUG [net.sf.hibernate.impl.SessionImpl] disconnecting session
2005-03-28 09:52:36,423 INFO [org.jboss.resource.connectionmanager.TxConnectionManager] throwable from unregister connection
java.lang.IllegalStateException: Trying to return an unknown connection2! org.jboss.resource.adapter.jdbc.WrappedConnection@200d0c
at org.jboss.resource.connectionmanager.CachedConnectionManager.unregisterConnection(CachedConnectionManager.java:330)
at org.jboss.resource.connectionmanager.TxConnectionManager$TxConnectionEventListener.connectionClosed(TxConnectionManager.java:539)
at org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection.closeHandle(BaseWrapperManagedConnection.java:296)
at org.jboss.resource.adapter.jdbc.WrappedConnection.close(WrappedConnection.java:117)
at net.sf.hibernate.connection.DatasourceConnectionProvider.closeConnection(DatasourceConnectionProvider.java:64)
at net.sf.hibernate.impl.BatcherImpl.closeConnection(BatcherImpl.java:297)
at net.sf.hibernate.impl.SessionImpl.disconnect(SessionImpl.java:3348)
at net.sf.hibernate.impl.SessionImpl.close(SessionImpl.java:576)
at com.pendylum.main.session.HibernateUtil.closeSession(HibernateUtil.java:134)
at com.pendylum.scheduler.ejb.ShiftManager.updateSchedule(ShiftManager.java:2248)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(StatelessSessionContainer.java:683)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:185)
at org.jboss.ejb.plugins.StatelessSessionInstanceInterceptor.invoke(StatelessSessionInstanceInterceptor.java:72)
at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:84)
at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:267)
at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:128)
at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:118)
at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:191)
at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122)
at org.jboss.ejb.StatelessSessionContainer.internalInvoke(StatelessSessionContainer.java:331)
at org.jboss.ejb.Container.invoke(Container.java:700)
at sun.reflect.GeneratedMethodAccessor55.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.mx.capability.ReflectedMBeanDispatcher.invoke(ReflectedMBeanDispatcher.java:284)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:546)
at org.jboss.invocation.local.LocalInvoker.invoke(LocalInvoker.java:101)
at org.jboss.invocation.InvokerInterceptor.invoke(InvokerInterceptor.java:90)
at org.jboss.proxy.TransactionInterceptor.invoke(TransactionInterceptor.java:46)
at org.jboss.proxy.SecurityInterceptor.invoke(SecurityInterceptor.java:45)
at org.jboss.proxy.ejb.StatelessSessionInterceptor.invoke(StatelessSessionInterceptor.java:100)
at org.jboss.proxy.ClientContainer.invoke(ClientContainer.java:85)
at $Proxy58.updateSchedule(Unknown Source)
at com.pendylum.scheduler.delegate.ShiftDelegate.updateSchedule(ShiftDelegate.java:150)
at org.apache.jsp.schScheduleUpdate_jsp._jspService(schScheduleUpdate_jsp.java:159)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:210)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.jboss.web.tomcat.security.JBossSecurityMgrRealm.invoke(JBossSecurityMgrRealm.java:220)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.jboss.web.tomcat.tc4.statistics.ContainerStatsValve.invoke(ContainerStatsValve.java:76)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2417)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:65)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:577)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:197)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:781)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:549)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:605)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:677)
at java.lang.Thread.run(Thread.java:534)
2005-03-28 09:52:36,423 INFO [STDOUT] HibernateUtil: closeSession() successfully executed.
2005-03-28 09:52:36,423 DEBUG [net.sf.hibernate.engine.CacheSynchronization] transaction before completion callback
2005-03-28 09:52:36,423 DEBUG [net.sf.hibernate.engine.CacheSynchronization] transaction after completion callback, status: 4
2005-03-28 09:52:36,423 DEBUG [net.sf.hibernate.impl.SessionImpl] transaction completion
2005-03-28 09:52:36,423 ERROR [org.jboss.ejb.plugins.LogInterceptor] RuntimeException:
java.lang.ArrayIndexOutOfBoundsException: 3
at com.pendylum.scheduler.process.TemplateAutomation.automateTemplateForSchedule(TemplateAutomation.java:129)
at com.pendylum.scheduler.ejb.ShiftManager.updateSchedule(ShiftManager.java:2227)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(StatelessSessionContainer.java:683)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:185)
at org.jboss.ejb.plugins.StatelessSessionInstanceInterceptor.invoke(StatelessSessionInstanceInterceptor.java:72)
at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:84)
at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:267)
at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:128)
at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:118)
at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:191)
at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122)
at org.jboss.ejb.StatelessSessionContainer.internalInvoke(StatelessSessionContainer.java:331)
at org.jboss.ejb.Container.invoke(Container.java:700)
at sun.reflect.GeneratedMethodAccessor55.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.mx.capability.ReflectedMBeanDispatcher.invoke(ReflectedMBeanDispatcher.java:284)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:546)
at org.jboss.invocation.local.LocalInvoker.invoke(LocalInvoker.java:101)
at org.jboss.invocation.InvokerInterceptor.invoke(InvokerInterceptor.java:90)
at org.jboss.proxy.TransactionInterceptor.invoke(TransactionInterceptor.java:46)
at org.jboss.proxy.SecurityInterceptor.invoke(SecurityInterceptor.java:45)
at org.jboss.proxy.ejb.StatelessSessionInterceptor.invoke(StatelessSessionInterceptor.java:100)
at org.jboss.proxy.ClientContainer.invoke(ClientContainer.java:85)
at $Proxy58.updateSchedule(Unknown Source)
at com.pendylum.scheduler.delegate.ShiftDelegate.updateSchedule(ShiftDelegate.java:150)
at org.apache.jsp.schScheduleUpdate_jsp._jspService(schScheduleUpdate_jsp.java:159)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:210)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.jboss.web.tomcat.security.JBossSecurityMgrRealm.invoke(JBossSecurityMgrRealm.java:220)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.jboss.web.tomcat.tc4.statistics.ContainerStatsValve.invoke(ContainerStatsValve.java:76)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2417)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:65)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:577)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:197)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:781)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:549)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:605)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:677)
at java.lang.Thread.run(Thread.java:534)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 28, 2005 3:36 pm 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
Yes, because in your catch block, you are trying to close the session, and the session is trying to return a connection that it maybe never borrowed. Maybe put a try / catch around this (and ignore exception):

at com.pendylum.main.session.HibernateUtil.closeSession(HibernateUtil.java:134)


Though I dont know your error handling strategy, so I dont know if that is a good idea or not. though typically if the error handling is just around the return of the connection to pool, or if you can check to see if the connection was really borrowed from the pool, you should be ok.

Chris


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 30, 2005 3:46 pm 
Beginner
Beginner

Joined: Thu Jan 13, 2005 10:31 am
Posts: 20
Quote:
because in your catch block, you are trying to close the session


But I am trying to close the session in my finally block not in catch block.

try {
HibernateUtil.getSession();
HibernateUtil.beginTransaction();

// do 1st task
....

HibernateUtil.commitTransaction();

// call 2nd task
String returnMessage = ....

} catch (...) {
...
HibernateUtil.rollbackTransaction();
} finally {
System.out.println("Inside the finally block");
HibernateUtil.closeSession();
}

Quote:
Maybe put a try / catch around this (and ignore exception):

at com.pendylum.main.session.HibernateUtil.closeSession(HibernateUtil.java:134)



HibernateUtil is using ThreadLocal pattern. This is the method to close the Session:

public static void closeSession() {
try {
Session s = (Session)threadSession.get();
threadSession.set( null );
if ( s != null && s.isOpen() ) {
System.out.println( "HibernateUtil: Closing Session of this thread." );
s.close();
System.out.println( "HibernateUtil: closeSession() successfully executed." );
}
} catch ( HibernateException hex ) {
System.out.println( "HibernateUtil: closeSession() failed." );
hex.printStackTrace();
}
}


The line 134 is :
s.close();

As you can see I already have a try and catch block. Any suggestion?

Thanks

Uryl


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 07, 2005 12:54 am 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
OK, in your original log I see the arrayOutOfBounds Exception... so it looks like it is going to the catch block of the close connection, logging and ignoring the exception, then continuing to fail on the outOfBounds Exception... so it is ok, right?

2005-03-28 09:52:36,423 ERROR [org.jboss.ejb.plugins.LogInterceptor] RuntimeException:
java.lang.ArrayIndexOutOfBoundsException: 3

Also, in your close method, I think it is a pooling exception, having to do with JBoss:

java.lang.IllegalStateException

So you might want to catch Exception there, and not HibernateException so the method does not fail.

Regards,
Chris


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

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.