Can't pass connectiion reference between beans
camus Jul 15, 2002 5:35 AMHi ALL,
Finally, I can create the connectionpool, connect to Oracle server and get the sql statement works within the same bean. However, exceptions occur when pass between beans. Here's information and scenario:
Environment:
JBoss 3.0.0
Oracle 8i
JDK 1.3
SUNOS 5.7
Problem:
1. Suppose bean_1 (session bean, stateless) has a common function (getDBConn) to lookup the datasource and return a connection for Oracle 8i, the code:
InitialContext iCtx = new InitialContext();
DataSource ds = (DataSource) iCtx.lookup ("java:/OracleDS");
Connection con = ds.getConnection();
return con ;
2. Another bean bean_2 (session bean, stateless) uses jndi to lookup bean_1 and call bean_1's getDBConn method and get a connection reference and use that connection to do database operation:
try {
....
Connection con = bean_1.getDBConn() ;
PreparedStatement pstmt con.prepareStatement("select ...") ;
} catch(Exception e) {...}
3. The line "PreparedStatement pstmt con.prepareStatement..." (in fact, any database operation with that connection reference, like con.setautocommit(true)) ALWAYS has exception:
2002-07-15 16:22:04,819 ERROR [STDERR] java.sql.SQLException: Connection handle is not currently associated with a ManagedConnection
2002-07-15 16:22:04,820 ERROR [STDERR] at org.jboss.resource.adapter.jdbc.local.LocalConnection.checkStatus(LocalConnection.java:774)
2002-07-15 16:22:04,821 ERROR [STDERR] at org.jboss.resource.adapter.jdbc.local.LocalConnection.prepareStatement(LocalConnection.java:188)
4. However, if in step (1), I use (instead of datasource):
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
con = DriverManager.getConnection("jdbc:oracle:thin:@db-serv:1521:bv","test","test");
return con ;
} ....
Then no exceptions in bean_2
5. If run the code in step (2) in bean_1, then there is also no exceptions too !!!
Question: how can I call a common bean to get connection reference (by lookup datasource method) and use that connection to do database operations in another bean ? Is it an application level exceptions or server level or configuration level or security level problem ?
Many thanks !