如何在 try-catch 块中重新连接到数据库?
How to reconnect to database within try-catch block?
我有一个 java 应用程序,它使用 Spring JDBCTemplate 对 MySQL 数据库执行连续不间断的快速插入。几分钟后,显然数据库发生了一些事情,导致数据库连接中断,我得到一个异常
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
我能够在代码中捕获 SQLException,所以此时我想重新建立数据库连接并退出 catch 块并继续。
有什么方法可以做到这一点?
编辑:这是捕获异常的方法:
public int insertRecord(final String sql,final Object[] paramArray, KeyHolder keyHolder) {
Integer retStatus = jdbcTemplate.update(new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection con) {
String[] keyColNames=new String[1];
PreparedStatement ps = null;
try {
ps=con.prepareStatement(sql,keyColNames);
if(paramArray!=null){
int size=paramArray.length;
for(int i=0;i<size;i++){
ps.setObject(i+1, paramArray[i]);
}
}
} catch (CannotGetJdbcConnectionException e) {
logger.debug("caught SQLexception, now what should I do?");
con.reconnectToDatabase(); // this method doesnt exist but is what I need!
}
return ps;
}
}, keyHolder);
return retStatus;
}
更新:
如果您希望简单地重新连接数据库,您可以专门为此创建一个 class,为它创建一个 getConnection 方法,然后简单地 return re-established 连接.
例如:
public class ConnectionManager {
private Connection connection;
public String driverURL = "[Your driver URL]";
public Connection getConnection(String user, String password) {
try {
//SQLServerDriver or whichever you are using
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection = DriverManager.getConnection(driverURL, username, password);
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
return connection;
}
然后:
con = new ConnectionManager().getConnection(user, pass);
从那里您可以再次调用该方法并重试插入或继续进行。
我有一个 java 应用程序,它使用 Spring JDBCTemplate 对 MySQL 数据库执行连续不间断的快速插入。几分钟后,显然数据库发生了一些事情,导致数据库连接中断,我得到一个异常
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
我能够在代码中捕获 SQLException,所以此时我想重新建立数据库连接并退出 catch 块并继续。
有什么方法可以做到这一点?
编辑:这是捕获异常的方法:
public int insertRecord(final String sql,final Object[] paramArray, KeyHolder keyHolder) {
Integer retStatus = jdbcTemplate.update(new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection con) {
String[] keyColNames=new String[1];
PreparedStatement ps = null;
try {
ps=con.prepareStatement(sql,keyColNames);
if(paramArray!=null){
int size=paramArray.length;
for(int i=0;i<size;i++){
ps.setObject(i+1, paramArray[i]);
}
}
} catch (CannotGetJdbcConnectionException e) {
logger.debug("caught SQLexception, now what should I do?");
con.reconnectToDatabase(); // this method doesnt exist but is what I need!
}
return ps;
}
}, keyHolder);
return retStatus;
}
更新:
如果您希望简单地重新连接数据库,您可以专门为此创建一个 class,为它创建一个 getConnection 方法,然后简单地 return re-established 连接. 例如:
public class ConnectionManager {
private Connection connection;
public String driverURL = "[Your driver URL]";
public Connection getConnection(String user, String password) {
try {
//SQLServerDriver or whichever you are using
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection = DriverManager.getConnection(driverURL, username, password);
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
return connection;
}
然后:
con = new ConnectionManager().getConnection(user, pass);
从那里您可以再次调用该方法并重试插入或继续进行。