Raja Dhasan
Raja Dhasan

Reputation: 593

Unknown database and No operations allowed after connection closed

I have a xml file which I use to get DB infomation and about database through a Java class(DB_Conn).In DB_Conn I have a getconnection() which reads the xml file and starts a conncetion like below

my_conn_obj = DriverManager.getConnection(my_Path.item(0).getNodeValue() + my_DBname.item(0).getNodeValue(), my_User.item(0).getNodeValue(), my_Pass.item(0).getNodeValue());

.In that method I return a connection object(my_conn_obj) .I have defined it has null at the start of the class Then In a separate Java class I use

DB_Conn db= New DB_Conn();

And whenever I need/open a connection I use

Connection conn;
conn=db.getconnection();

I have closed all the connection and have put try catch properly.It worked normally, but when I am idle for some time and then when I refresh the page I sometimes and not always get Unknown database name "null" and No operations allowed after connection closed exceptions.

Upvotes: 1

Views: 146

Answers (1)

Jacques Ramsden
Jacques Ramsden

Reputation: 871

What is probably happening is that the connection is left open and is timing out, or you have closed it and need to re-connect it.

So when you use

Connection conn;
conn=db.getconnection();

Make sure that in getConnection you do something like

public Connection getConnection() {
  if (classRefToConnection == null || classRefToConnection.isClosed()) {
    //RE create or connect the connection before returning
  }
}

Upvotes: 2

Related Questions