Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to keep MySQL connection alive via JDBC Driver

Status
Not open for further replies.

chunkII123

IS-IT--Management
Mar 18, 2009
78
0
0
US
I am currently looking for a way to force the connection to our new MySQL server to stay alive. It seems to have a five minute inactivity timeout, and I am looking for a way to force it to eith indefinitly auto-reconnect silently upon disconnection, or to permanently stay alive while it is not in use. Here is the code that I currently use to connect to our old server.

Code:
public Connection getConnection() {
        try {
            if (dbConnection == null || dbConnection.isClosed()) {
               class.forName("com.mysql.jdbc.Driver");
                
               url = "jdbc:mysql://******/mmimembersinfo";
                userName = "*********";
                password = "*********";

                Connection con = DriverManager.getConnection(url, userName, password);
                
                System.out.println("URL: " + url + "\nConnection: " + con);
                
                dbConnection = con;
                return con;
            } else return dbConnection;
        }catch( Exception exc ) {           
            exc.printStackTrace();
            WarningMessage warning = new WarningMessage(true, "Error: " + CLASS_NAME + " " + exc.getMessage(), WarningMessage.CRITICAL_ERROR);
            warning.setVisible(true);
            return null;
        }
    }

On a side note, the username, password, and actual database IP address is known, however, I blanked these fields out with *'s.

Beware of hackers bearing executables. Happy Hunting. 'irc.2600.net'
 
I would like to keep the connection alive, because the server times out if there is no activity. When the server times out, it causes the program to freeze, and forcibly be shutdown, and restarted.

Beware of hackers bearing executables. Happy Hunting. 'irc.2600.net'
 
I don't think the server is timing out, I guess there is a problem in the program that makes it freeze.

Maybe the connection is timing out and you're trying to reuse it without checking it's alive.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top