У меня есть приложение, которое я подключаю к базе данных 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;
}
Что я могу изменить в любом из этих методов, чтобы приспособиться к потере соединения?