Can I keep an open JDBC connection through the life of state
moonrainbow Jul 28, 2003 12:03 PMI'd like to keep an open JDBC connection in a stateless session bean. The reason is to avoid creation/compilation of a statement every time a request comes to the bean. I use JBoss 3.0.7 and Oracle 8.
In ejbCreate I fetch a connection from the pool and prepare a statement:
InitialContext lContext = new InitialContext();
String lDataSourceName = (String) lContext.lookup(
"java:comp/env/BVDataSourceName"); // hardcoded, mentioned in jboss.xml
DataSource ds = (DataSource) lContext.lookup(lDataSourceName);
c = ds.getConnection();
decypherCall = c.prepareCall("{...}");
I tried both bean managed transaction demarcation and container managed t.d.
For bean managed t.d. the code of a business method looks like this:
...
javax.transaction.UserTransaction ut;
String ret="";
ut = sessionContext.getUserTransaction();
// start the transaction
try
{
ut.begin();
decypherCall.setString(2, encrypted);
boolean b = decypherCall.execute();
ret = decypherCall.getString(1);
decypherCall.clearParameters();
ut.commit();
}
catch (Exception ex)
{
log.error("Could not decypher CC number.",ex);
}
...
For container managed t.d. I used "NotSupported" tansaction-type and the code looks the same with the
UserTransaction calls removed.
Either method works only ones - after current transaction is completed I'm getting the following exception and the connection is dead. What am I doing wrong? Is such approach possible in Jboss?
2003-07-25 16:34:20,288 INFO [org.jboss.resource.connectionmanager.CachedConnectionManager] Throwable trying to close a connection for you, please close it yourself
java.lang.reflect.InvocationTargetException
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.resource.connectionmanager.CachedConnectionManager.closeAll(CachedConnectionManager.java:376)
at org.jboss.resource.connectionmanager.CachedConnectionManager.popMetaAwareObject(CachedConnectionManager.java:199)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:190)
at org.jboss.ejb.plugins.StatefulSessionInstanceInterceptor.invoke(StatefulSessionInstanceInterceptor.java:268)
at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:107)
at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:237)
at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:98)
at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:208)
at org.jboss.ejb.StatefulSessionContainer.invoke(StatefulSessionContainer.java:380)
at org.jboss.ejb.Container.invoke(Container.java:738)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:517)
at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:383)
at sun.reflect.GeneratedMethodAccessor198.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:261)
at sun.rmi.transport.Transport$1.run(Transport.java:148)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
at java.lang.Thread.run(Thread.java:536)
Caused by: java.lang.IllegalArgumentException: xaRes not enlisted
at org.jboss.tm.TxCapsule.delistResource(TxCapsule.java:595)
at org.jboss.tm.TransactionImpl.delistResource(TransactionImpl.java:92)
at org.jboss.resource.connectionmanager.LocalTxConnectionManager$LocalConnectionEventListener.delist(LocalTxConnectionManager.java:347)
at org.jboss.resource.connectionmanager.LocalTxConnectionManager$LocalConnectionEventListener.connectionClosed(LocalTxConnectionManager.java:389)
at org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection.closeHandle(BaseWrapperManagedConnection.java:280)
at org.jboss.resource.adapter.jdbc.WrappedConnection.close(WrappedConnection.java:97)
... 26 more