I have a DAO class which have many methods manipulating the database. I am using one connection object for all methods like this (Database.connect() returns a connection object):
class ExampleDAOImpl implements ExampleDAO{
private Connection con = null;
public void method1 () {
con = Database.connect();
....
con.close();
}
public void method2 () {
con = Database.connect();
....
con.close();
}
public void method1 () {
con = Database.connect();
....
con.close();
}
}
Is this a good practice to instantiate a new connection for each method and close it? I am having now errors saying " No operations allowed after connection closed" Although I am initializing the connection at the beginning of each method and closing it at the end. Or it's better to use the same connection object and have a separate method which closes it when i call it?