File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connection scope

 
nikki lorenzo
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Would there be any difference if I put the Connection reference in the class scope but new connection is retrieved for every method versus having it inside the method scope? Assuming, we are using Connection Pooling? Thanks.

1) Connection in class scope
public class MyDAO{
private Connection conn = null;

public void openDAO() {
if (this.conn == null) {
this.conn = WebConnectionFactory.getConnection();
}
}

public void myMethod(){
openDAO();
conn.prepareStatment("Select * from table");
//get resultSet
closeDAO();
}

public void mySecondMethod(){
openDAO();
conn.prepareStatment("Select * from table");
//get resultSet
closeDAO();
}
public void closeDAO(){
conn.close();
}
}

2) Connection in method scope
public class MyOtherDAO{
public myOtherMethod(){
Connection conn = WebConnectionFactory.getConnection();
conn.prepareStatment("Select * from table");
//get resultSet
conn.close()
}

public mySecondMethod(){
Connection conn = WebConnectionFactory.getConnection();
conn.prepareStatment("Select * from table");
//get resultSet
conn.close()
}
}
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the advantage you get out of this exercise?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If conn were used in a number of methods, having a member variable might feel cleaner than passing a parameter all over the place. But it is not thread safe, if that matters. Oh, and closeDAO() needs to set conn back to null, I think.
 
Jan Cumps
Bartender
Posts: 2575
11
C++ Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're supposed to return the connection to the pool as soon as possible (and reasonable). You should certainly not keep a reference to the connection you retrieved from the pool.
Even though you call close(), the connection is not closed, but returned to the pool. But you still have your attribute conn pointing to it.

  • You can get into trouble when the pool decides to shrink, or to regenerate it's connections.
  • When the pool hands over your returned connection to another piece of code, and you do a commit (or a rollback!) in your class with the dangling reference, you're interfering with the operation of that other code.

  • Best practice: Ask a connection, use it, return it.

    Regard, Jan
     
    Henrique Ordine
    Ranch Hand
    Posts: 128
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Additionally, your code will execute the test (this.conn == null) everytime a method of your DAO is called.
    I would definitely go for the method local variables.

     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic