4 Replies Latest reply on Nov 7, 2001 1:56 PM by Sundaram Ramasamy

    Accessing connection pool from Mbean

    Sundaram Ramasamy Newbie

      Hi,

      I want to get the database connction from my Mbean. I try to use the follwoing code, But it giving me the execpetion.

      Context ctx = new InitialContext();
      DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/DefaultDS");
      conn = ds.getConnection();

      Any help to get the DB Connection.

      Thanks
      SR

        • 1. Re: Accessing connection pool from Mbean
          Allen Fogleson Newbie

          just a guess but instead of "java:comp/env/jdbc/DefaultDS" have you tried java:/DefaultDS?

          Al

          • 2. Re: Accessing connection pool from Mbean
            Sundaram Ramasamy Newbie

            Yes, I tried but its not working.
            -SR

            • 3. Re: Accessing connection pool from Mbean
              Sundaram Ramasamy Newbie

              I have written stateless session bean to get the DB Connection, its working with MBean, but not working with servlet running on other application scope in the same JVM.


              MY SLSB looks like this.

              Remote Interface
              ------------------

              package com.percipia.db.ejb;
              import java.rmi.RemoteException;
              import java.sql.*;
              import javax.ejb.*;

              public interface GetDBConnection extends EJBObject {

              public Connection GetConnection() throws RemoteException;

              }

              Home Interface
              --------------

              package com.percipia.db.ejb;
              import java.rmi.RemoteException;

              import javax.ejb.*;


              public interface GetDBConnectionHome extends EJBHome
              {
              public GetDBConnection create () throws CreateException, RemoteException;
              }

              Bean
              -----
              package com.percipia.db.ejb;

              import java.rmi.RemoteException;

              import java.sql.*;

              import javax.naming.*;
              import javax.ejb.*;
              import javax.sql.DataSource;

              public class GetDBConnectionBean implements SessionBean {


              SessionContext ctx = null;
              private DataSource ds = null;

              public GetDBConnectionBean() {
              }

              public void ejbCreate () throws CreateException, RemoteException {
              System.out.println("Create method called");
              try {
              // ds = (DataSource)new InitialContext().lookup("jdbc/DefaultDS");
              ds = (DataSource)new InitialContext().lookup("java:comp/env/jdbc/DefaultDS");

              System.out.println(" ejb create Got the connection");
              } catch (NamingException _ne) {
              System.out.println("ejbcreate error " +_ne.toString());
              throw new CreateException ("Datasource not found: "+_ne.getMessage ());
              }
              }

              public Connection GetConnection() throws RemoteException {
              System.out.println("Got the Request");
              try {
              return ds.getConnection();
              } catch( Exception ex ) {
              System.out.println("Error:" + ex);
              throw new RemoteException( ex.getMessage() );
              }
              }
              public void ejbActivate () {}
              public void ejbPassivate () {}
              public void ejbRemove () {}
              public void setSessionContext (SessionContext _ctx) {ctx = _ctx;}

              }

              ejb-jar.xml
              -----------

              <ejb-name>GetDBConnection</ejb-name>
              com.percipia.db.ejb.GetDBConnectionHome
              com.percipia.db.ejb.GetDBConnection
              <ejb-class>com.percipia.db.ejb.GetDBConnectionBean</ejb-class>
              <session-type>Stateless</session-type>
              <transaction-type>Container</transaction-type>
              <resource-ref>
              <res-ref-name>jdbc/DefaultDS</res-ref-name>
              <res-type>javax.sql.DataSource</res-type>
              <res-auth>Container</res-auth>
              </resource-ref>



              jboss.xml
              ---------
              <enterprise-beans>

              <ejb-name>GetDBConnection</ejb-name>
              <jndi-name>ejb/GetDBConnection</jndi-name>



              </enterprise-beans>
              <resource-ref>
              <res-ref-name>jdbc/DefaultDS</res-ref-name>
              <jndi-name>java:/DefaultDS</jndi-name>
              </resource-ref>

              Clinet Code:
              -----------
              public void getSQL() {
              try {
              System.out.println("I am in getSQL");
              // Get a naming context
              Properties p = new Properties();
              p.put( Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
              p.put( Context.PROVIDER_URL, "localhost:1099");

              InitialContext jndiContext = new InitialContext(p);

              Object ref = jndiContext.lookup("ejb/GetDBConnection");
              System.out.println("Got the home interface");

              // Get a reference from this to the Bean's Home interface
              GetDBConnectionHome home = (GetDBConnectionHome)
              PortableRemoteObject.narrow (ref, GetDBConnectionHome.class);
              GetDBConnection db = home.create();
              java.sql.Connection con = db.GetConnection();
              // Query the employee names
              Statement stmt = con.createStatement ();
              ResultSet rset = stmt.executeQuery ("select * from room");

              // Print the name out
              while (rset.next ()) {

              String roomid= rset.getString(1);
              System.out.println("room " + roomid);
              }
              con.close();
              System.out.println("Got the Connection.");
              } catch( Exception ex ) {
              System.out.println("Error :" + ex);
              }

              }

              I am using this in Mbean its working. Same code in same JVM with servlet i am getting follwoing error.

              [Default] Error :java.lang.reflect.UndeclaredThrowableException
              [Default] java.lang.reflect.UndeclaredThrowableException:
              [Default] java.io.NotSerializableException: org.jboss.pool.jdbc.xa.wrapper.XACli
              entConnection
              [Default] <<no stack trace available>>
              [Default]

              Any Help with Servlet.

              Thanks
              -SR

              • 4. Re: Accessing connection pool from Mbean
                Sundaram Ramasamy Newbie

                Hi,

                After looking the org.jboss.jdbc.JDBCDataSourceLoader source code, I was able to get the DB Connection from my servlet running in the same JVM but different application scope.

                log file I got the pool name java:/DefaultDS and java:/mySQL.


                I have used the follwoing code to get the Connection object.

                Properties p = new Properties();
                p.put( Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
                p.put( Context.PROVIDER_URL, "localhost:1099");

                Context ctx = new InitialContext(p);

                DataSource ds = (DataSource) ctx.lookup("java:/mySQL");
                conn = ds.getConnection();



                [XADataSourceLoader] Starting
                [DefaultDS] XA Connection pool DefaultDS bound to java:/DefaultDS
                [XADataSourceLoader] Started
                [XADataSourceLoader] Starting
                [mySQL] XA Connection pool mySQL bound to java:/mySQL
                [XADataSourceLoader] Started

                Cheers
                -SR