У меня есть приложение, которое я подключаю к базе данных MySQL. Он теряет соединение в середине ночи, а затем отключает соединения null
, а JDBC не получает сообщений за X секунд.
Я вызываю getConnection()
, прежде чем делать что-либо, требующее связи с SQL-сервером.
Это мой метод getConnection()
:
private Connection getConnection() {
try {
if (connection != null) {
if (connection.isClosed() || !connection.isValid(10000)) {
this.initializeRamsesConnection();
}
} else {
this.initializeRamsesConnection();
}
} catch (Exception e) {
debug("Connection failed: " + e);
}
return connection;
}
В методе initializeRamsesConnection()
я помещаю пароль и т.д. в строку, а затем создаю соединение стандартным способом JDBC.
Затем я вызываю этот метод:
private Connection getConnectionFromConnectionString() {
Connection con = null;
String driver = "com.mysql.jdbc.Driver";
try {
Class.forName(driver);//jdbc sorcery
//if there is no connection string
if (getConnectionString() == null) {
HMIDatabaseAdapter.debug("No connection string");
}
//makes a string out of the values of db/host
String str = getConnectionString();
//if there is no driver
if (driver == null) {
debug("" + ": " + "No driver");
}
//Tries to make a connection from the connection string, username, and the password.
con = DriverManager.getConnection(str, username, password);
//if for some reason the connection is null
if (con == null) {
HMIDatabaseAdapter.debug("CONNECTION IS NULL, WHAT?");
}
} catch (Exception ex) {
HMIDatabaseAdapter.debug("getConnection() " + ex);
}
return con;
}
Что я могу изменить в любом из этих методов для размещения потерянного соединения?