4 Replies Latest reply on Feb 26, 2002 10:54 PM by jian gao

    How can I get a connection from a connection pool?

    Fco Javier Newbie

      Hi everybody!

      I'm developing an Entity Bean Managed EJB with JBoss 2.4, and I need to access to the connection pool and get a transactional connection from it. How can i do it?
      I suppose I need to instance a jts driver and then get the connection using something like this:
      Connection con = DriverManager.getConnection("url...");
      But I don't know which driver and the url I shall use.

      Alternative ways to get a transactional connection from a pool to use it inside an EJB method will be regarded.

      Thanks in advance!
      You could email to [link=mailto:javiergarcia@softhome.net]javiergarcia@softhome.net[/link]

        • 1. Re: How can I get a connection from a connection pool?
          Fco Javier Newbie

          Help is not needed. I found it.

          Thanks.

          • 2. Re: How can I get a connection from a connection pool?
            Fco Javier Newbie

            I use the following private method to get a pool connection. I don't know if it a JTS connection. Does anyone know if it does?

            private Connection getConnection() throws SQLException {
            try
            {
            Context jndiContext = new InitialContext();
            DataSource ds = (DataSource)jndiContext.lookup("java:/poolName");
            return ds.getConnection();
            }
            catch (Exception ex)
            {
            throw new EJBException(ex);
            }
            }

            • 3. Re: How can I get a connection from a connection pool?
              jian gao Newbie

              I think maybe it is a good idea.But I want to know more detail about jndi name:

              if in jboss.jcml ,there is :

              should I use:
              lookup("java:/InstantDB") ?

              • 4. Re: How can I get a connection from a connection pool?
                jian gao Newbie

                I think maybe there still be some puzzle, I have an example,I make an EJB:
                package quay;
                import javax.ejb.*;
                import javax.sql.XAConnection;
                import javax.sql.XADataSource;
                import javax.naming.InitialContext;
                import javax.naming.Context;

                public class myquayBean implements SessionBean{
                public void ejbCreate(){}
                public void ejbActivate(){}
                public void ejbPassivate(){}
                public void ejbRemove(){}
                public void setSessionContext(SessionContext ctx){}

                public String say()
                {
                //return "Hi,my quay!";
                try{
                XAConnection myconn=getConnection();
                if (myconn!=null){
                return "Hi, my quay!";
                }else{
                return "conn null!";
                }
                }catch(Exception e){
                return "Exception!";
                }
                }

                private XAConnection getConnection() throws Exception {
                try{
                Context jndiContext = new InitialContext();
                XADataSource ds = (XADataSource)jndiContext.lookup("java:/InstantDB");
                return ds.getXAConnection();
                }catch (Exception ex){
                ex.printStackTrace();
                throw new EJBException(ex);
                }
                }
                }

                after deploy and run, I found that the jboss console says:

                java.lang.ClassCastException: org.opentools.minerva.jdbc.xa.XAPoolDataSource

                Then, should I cast the jndi lookup result to
                org.opentools.minerva.jdbc.xa.XAPoolDataSource ?