RiazKhanmohamed
Programmer
- Oct 30, 2002
- 115
Managed to close my connection. my current connectionmanager class looks like:
***CODE START***
import java.sql.*;
public class ConnectionManager{
private static ConnectionManager conMan;
private Connection con;
private ConnectionManager(){
try {
// Load the Oracle Driver
Class.forName("oracle.jdbc.driver.OracleDriver");
try{
// Get a connection from the connection factory
con = DriverManager.getConnection("jdbc
racle:thin
[servername]:1521:dec", "[username]", "[password]");
}
catch (SQLException e){
System.out.println("Cannot connect");
System.out.println(e.getMessage());
}
}
catch (ClassNotFoundException ex){
System.out.println("Cannot find driver");
//System.exit(-1);
}
}
public static ConnectionManager getInstance(){
if(conMan == null)
conMan = new ConnectionManager();
return conMan;
}
public Connection getConnection(){
return con;
}
}
***CODE STOPS****
works fine.
i now have multiple mappers. each has a connection function:
***CODE START***
public void conConnect()throws SQLException{
ConnectionManager man = ConnectionManager.getInstance();
con = man.getConnection();
}
***CODE STOPS***
and a close connection function:
***CODE STARTS***
public void conClose() throws SQLException{
if (!con.isClosed()){
con.close();
}
}
***CODE STOPS***
I have multiple functions in my middle/problem domain running these queries - each makes a connection, runs several queries, then closes the connection.
THE PROBLEM:
Once the first function closes the connection, any subsequent connectoin creations that occur don't work, refusing access to the database.
Before i simply created the connection on creation of the database mapper instance, but this keps a single connectoin open permanently, which eventually timed out - i know i had to close it somewhere so this is what i have.
Please note code is valid, and it connects/disconnects the first time round, but never again.
Please help, this is really starting to bug me badly!
Your assistance is greatly appreciated.
***CODE START***
import java.sql.*;
public class ConnectionManager{
private static ConnectionManager conMan;
private Connection con;
private ConnectionManager(){
try {
// Load the Oracle Driver
Class.forName("oracle.jdbc.driver.OracleDriver");
try{
// Get a connection from the connection factory
con = DriverManager.getConnection("jdbc
}
catch (SQLException e){
System.out.println("Cannot connect");
System.out.println(e.getMessage());
}
}
catch (ClassNotFoundException ex){
System.out.println("Cannot find driver");
//System.exit(-1);
}
}
public static ConnectionManager getInstance(){
if(conMan == null)
conMan = new ConnectionManager();
return conMan;
}
public Connection getConnection(){
return con;
}
}
***CODE STOPS****
works fine.
i now have multiple mappers. each has a connection function:
***CODE START***
public void conConnect()throws SQLException{
ConnectionManager man = ConnectionManager.getInstance();
con = man.getConnection();
}
***CODE STOPS***
and a close connection function:
***CODE STARTS***
public void conClose() throws SQLException{
if (!con.isClosed()){
con.close();
}
}
***CODE STOPS***
I have multiple functions in my middle/problem domain running these queries - each makes a connection, runs several queries, then closes the connection.
THE PROBLEM:
Once the first function closes the connection, any subsequent connectoin creations that occur don't work, refusing access to the database.
Before i simply created the connection on creation of the database mapper instance, but this keps a single connectoin open permanently, which eventually timed out - i know i had to close it somewhere so this is what i have.
Please note code is valid, and it connects/disconnects the first time round, but never again.
Please help, this is really starting to bug me badly!
Your assistance is greatly appreciated.