1 Reply Latest reply on Dec 2, 2002 12:01 PM by Wouter Zoons

    Jboss connection pools and JDBC

    TMY Newbie

      Hello

      I would like to know about Jboss database connection pools. I am aware that Jboss will create many connections to a database and hold them in a pool until a user requires it. I would like to know more about connection pools and JDBC

      For the following scenario.

      1. User1 creates an instance of SessionBean
      2. User2 creates another instance of SessionBean at the same time as User1
      3. User1 and User2 both access the database using JDBC at the same time.

      So I would like to know if Jboss could allow the same database connection object to be used between the two simultaneous users or not. Intuitively it makes sense not to allow this since each instance of a SessionBean could be doing their own transactions.

      I need to know this as I am using oracle global temporary tables, and if it is possible that jboss would allow the sharing of the same connection object among simultaneous users, then this will render the feature of global temporary tables unusable.

      Thanks

        • 1. Re: Jboss connection pools and JDBC
          Wouter Zoons Newbie

          hi,

          first of all it is important to treat stateless and stateful session beans differently.

          Stateless session beans do not maintain state information. All instances of a stateless session bean are considered to be identical (you can have more in a clustered environment for example), typically you allocate and release the DB connection in the (short) lifetime of the bean, this can be real a performance killer since there is no shared resource and you introduce the overhead of creation/destruction each time.

          On the other hand, each instance of a particular stateful session bean is unique, and therefore each request for a connection to a datasource will _normally_ result in a new connection, as you already expected.

          For the stateful example you could code a singleton pattern (for this bean only) that caches the connection and returns the same instance once it has one, this way you would ask a connection to your bean instead of directly to the DataSource you are using (be it JBoss, Oracle, etc...)

          so, something like this:

          public class MyStatefulSessionBean ...
          {
          private static Connection connection = null;

          private synchronized Connection getConnection()
          {
          if (connection == null)
          {
          connection = dataSource.getConnection();
          }
          return connection;
          }
          }

          personally I try to avoid static members, especially in J2EE apps using complex ClassLoader hierarchies.

          Another alternative would be configuring your datasource to allow only one connection for the bean, dont ask me how , I am more of a CMP guy myself ;-)

          if somebody out there knows how to do this, I would like to know.

          anyway, to answer your question: I am pretty sure the JBoss implementation of the DataSource does not share connections by default for a session bean.

          cheers
          Wouter.