Help Connecting A Stateless Session Bean To MySQL Database
843833Oct 19 2005 — edited Nov 21 2005Hi, I'm trying to connect a simple stateless session bean that has been
deployed using the admin console to a MySQL database that has been
set up and pinged using the JDBC Resources/Connection Pools part of the
admin console.
When I attempt to connect to the database using the session bean I get the
following error message:
Exception in thread "main" java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.RemoteException: ; nested exception is:
java.sql.SQLException: No database selected
at com.sun.corba.ee.impl.javax.rmi.CORBA.Util.mapSystemException(Util.java:161)
at javax.rmi.CORBA.Util.mapSystemException(Unknown Source)
at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:142)
at com.sun.corba.ee.impl.presentation.rmi.bcel.BCELStubBase.invoke(Unknown Source)
at DBDynamicStub.testDatabase(_DB_DynamicStub.java)
at DBClient.main(DBClient.java:9)
Caused by: java.rmi.RemoteException: ; nested exception is:
java.sql.SQLException: No database selected
at com.sun.ejb.containers.InvocationHandlerUtil.throwRemoteException(InvocationHandlerUtil.java:96)
at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:141)
at $Proxy26.testDatabase(Unknown Source)
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:585)
at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:123)
at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatchToServant(CorbaServerRequestDispatcherImpl.java:648)
at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatch(CorbaServerRequestDispatcherImpl.java:192)
at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequestRequest(CorbaMessageMediatorImpl.java:1709)
at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:1569)
at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleInput(CorbaMessageMediatorImpl.java:951)
at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:181)
at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:721)
at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.dispatch(SocketOrChannelConnectionImpl.java:469)
at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.doWork(SocketOrChannelConnectionImpl.java:1258)
at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:409)
Caused by: java.sql.SQLException: No database selected
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2851)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1531)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1622)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2379)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2306)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1192)
at DBBean.testDatabase(DBBean.java:45)
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:585)
at com.sun.enterprise.security.SecurityUtil$2.run(SecurityUtil.java:153)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.enterprise.security.application.EJBSecurityManager.doAsPrivileged(EJBSecurityManager.java:950)
at com.sun.enterprise.security.SecurityUtil.invoke(SecurityUtil.java:158)
Obviously this means that the bean can't connect to the database but I am
unsure why this is. When I set up the database using the admin console
I set the the jndi name to jdbc/test and I've tried using the following look up
code to find the database that is referenced by the JNDI name:
Context context = new javax.naming.InitialContext();
DataSource ds = (DataSource) context.lookup("jdbc/test");
and also
Context context = new javax.naming.InitialContext();
DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/test");
Neither of these methods work.
When I look at the JNDI Tree Browsing window which is available from the
admin console there is no mention of the database at all, there are four
items in the three, they are:
UserTransaction
->jdbc
->ejb
DBBean
Do I need to add a JNDI reference somewhere in order for the bean to locate
the database? I thought that when I created the database in the admin
console and specified a JNDI name this would be all that's required in order
for beans to locate the database, obviously not.
If anyone can offer any advice at all I would be very very grateful.
Ian