1

How to get same connection from a DataSource in multiple classes? Is it possible?

Suppose I have a DataSource which I pass to 2 classes. The DataSource is using a Connection Pooling.

Then I call a method in 1st Class which get a Connection from the DataSource, uses that Connection and then closes that Connection.

Then I call a method in 2nd Class which get a Connection from the DataSource, uses that Connection and then closes that Connection.

Is it possible to be sure that the method in 2nd Class will get the same Connection which was used by method in 1st Class?

This is the example code:

This is the 1st class whose method will be called by the unit of work:

public class Class1 {
   private DataSource dataSource = null;

   public Class1(DataSource dataSource) {
      this.dataSource = dataSource;
   }

   public void class1Method1() throws Exception {
      Connection conn = null;
      try {
         conn = dataSource.getConnection();
         ... // do your work
      } finally {
         try {
            if (conn != null)
               conn.close();
         } catch (Exception e) {            
         } finally {
            conn = null;
         }
      }
   }
}

This is the 2nd class whose method will be called by the unit of work:

public class Class2 {
   private DataSource dataSource = null;

   public Class2(DataSource dataSource) {
      this.dataSource = dataSource;
   }

   public void class2Method1() throws Exception {
      Connection conn = null;
      try {
         conn = dataSource.getConnection();
         ... // do your work
      } finally {
         try {
            if (conn != null)
               conn.close();
         } catch (Exception e) {            
         } finally {
            conn = null;
         }
      }
   }
}

And this is my unit of work:

InitialContext initialContext = null;
DataSource dataSource = null;
Class1 class1 = null;
Class2 class2 = null;

initialContext = new InitialContext();
dataSource = (DataSource) initialContext.lookup("java:comp/env/jdbc/MyDB");
class1 = new Class1(dataSource);
class2 = new Class2(dataSource);
class1.class1Method1();
class2.class2Method1();
srh
  • 1,661
  • 4
  • 30
  • 57
  • 1
    If you close it after each, why would you expect to be able to reuse it? – Daniel Kaplan Mar 11 '15 at 22:19
  • What you seem to be asking for is a connection pool. Try Apache Commons DBCP. – user207421 Mar 11 '15 at 22:36
  • @DanielKaplan I was thinking about connection in transaction context. – srh Mar 11 '15 at 23:18
  • In that case why do you think you need to keep using the same connection? The only reason I can think of is that you want all the classes to be inside the same transaction. The solution to that is to pass the `Connection` around as a parameter: then you *know* it's the same connection. – user207421 Mar 11 '15 at 23:27

2 Answers2

1

Closing a connection releases the connection resources, see here and connections should always be closed. Depending on the DataSource implementation (e.g. connection pool) it could then for instance give that same connection back on the next getConnection call. But it does not have to do so, it can also terminate the first connection and return a new one each time.

For more info, see here and here.

Community
  • 1
  • 1
buftlica
  • 245
  • 1
  • 8
1

As @EJP said in a comment, You would need to pass the Connection around into the other class before you close it. You'd also need to make sure the second class doesn't close it if you plan to use it later in the outer class. This is a low level solution and quite a headache.

You can use Spring or Java EE to solve this problem at a higher level. It lets you mark all your relevant methods to say, "these should be transactional". If you call one for the first time, a new transaction is started. When you leave that outer method, the transaction automatically ends. But, if that method calls another transactional method, it knows the transaction is still open so it reuses it. It removes a lot of headache.

I believe these two technologies use Aspect Oriented Programming under the hood.

Read more for JEE: http://docs.oracle.com/javaee/6/tutorial/doc/bncih.html

Read more for Spring: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356