Java MySQL超时?Can';不说什么';怎么了,爪哇
我正在运行此方法以使用DBCP连接池更新SQL: 在整整8次之后,,方法setValue停止这样做,并且不发送任何数据Java MySQL超时?Can';不说什么';怎么了,爪哇,java,mysql,sql,apache-commons-dbcp,Java,Mysql,Sql,Apache Commons Dbcp,我正在运行此方法以使用DBCP连接池更新SQL: 在整整8次之后,,方法setValue停止这样做,并且不发送任何数据 public static void setValue(String user, Integer id){ Connection connection = null; try { try { connection = DataSource.getInstance().getConnection();
public static void setValue(String user, Integer id){
Connection connection = null;
try {
try {
connection = DataSource.getInstance().getConnection();
} catch (IOException e) {
e.printStackTrace();
} catch (PropertyVetoException e) {
e.printStackTrace();
}
ttl++;
PreparedStatement ps = connection.prepareStatement("REPLACE INTO " + "accounts"+ " (user,id) VALUES(?,?)");
ps.setString(1, user);
ps.setInt(2, id);
ps.executeUpdate();
ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return;
}
我不熟悉MySQL或连接池,我不知道这里出了什么问题。请帮我解决这个问题或提供任何建议?非常感谢你 使用try-catch-finally块,在try中提交连接,在finally和on-exception回滚中关闭准备好的语句
public static void setValue(String user, Integer id){
Connection connection = null;
try {
try {
connection = DataSource.getInstance().getConnection();
} catch (IOException e) {
e.printStackTrace();
} catch (PropertyVetoException e) {
e.printStackTrace();
}
ttl++;
PreparedStatement ps = connection.prepareStatement("REPLACE INTO " + "accounts"+ " (user,id) VALUES(?,?)");
ps.setString(1, user);
ps.setInt(2, id);
ps.executeUpdate();
connection.commit();
} catch (SQLException ex) {
connection.rollback();
ex.printStackTrace();
}
finally {
ps.close();
}
return;
}
使用try-catch-finally块,在try中提交连接,在finally和on-exception回滚中关闭准备好的语句
public static void setValue(String user, Integer id){
Connection connection = null;
try {
try {
connection = DataSource.getInstance().getConnection();
} catch (IOException e) {
e.printStackTrace();
} catch (PropertyVetoException e) {
e.printStackTrace();
}
ttl++;
PreparedStatement ps = connection.prepareStatement("REPLACE INTO " + "accounts"+ " (user,id) VALUES(?,?)");
ps.setString(1, user);
ps.setInt(2, id);
ps.executeUpdate();
connection.commit();
} catch (SQLException ex) {
connection.rollback();
ex.printStackTrace();
}
finally {
ps.close();
}
return;
}
我的猜测是,您正在使用池中的所有连接 试着把最后一块放进去
finally {
connection.close();
}
即使您正在使用连接池,您仍然必须关闭连接-只是连接池实际上并没有关闭它,它只是将它返回到池中,以便其他人可以使用它。我猜您只是在使用池中的所有连接 试着把最后一块放进去
finally {
connection.close();
}
即使您正在使用连接池,您仍然必须关闭连接-只是连接池实际上并没有关闭它,它只是将它返回到池中,以便其他人可以使用它。是的,您应该关闭连接,而这只会将它返回到池中。基本上,您应该在方法的finally{}块中添加这个。希望这有帮助
public static void setValue(String user, Integer id){
Connection connection = null;
try {
try {
connection = DataSource.getInstance().getConnection();
} catch (IOException e) {
e.printStackTrace();
} catch (PropertyVetoException e) {
e.printStackTrace();
}
ttl++;
PreparedStatement ps = connection.prepareStatement("REPLACE INTO " + "accounts"+ " (user,id) VALUES(?,?)");
ps.setString(1, user);
ps.setInt(2, id);
ps.executeUpdate();
ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
if (connection != null) connection.close();
}
return;
}
是的,您应该关闭您的连接,这将简单地将其返回到池中。基本上,您应该在方法的finally{}块中添加这个。希望这有帮助
public static void setValue(String user, Integer id){
Connection connection = null;
try {
try {
connection = DataSource.getInstance().getConnection();
} catch (IOException e) {
e.printStackTrace();
} catch (PropertyVetoException e) {
e.printStackTrace();
}
ttl++;
PreparedStatement ps = connection.prepareStatement("REPLACE INTO " + "accounts"+ " (user,id) VALUES(?,?)");
ps.setString(1, user);
ps.setInt(2, id);
ps.executeUpdate();
ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
if (connection != null) connection.close();
}
return;
}
您在哪里关闭语句(可能是连接)并将其返回到池中?我想我不是。我不确定如何使用池处理语句。您的连接池是否正好有8个连接?您的
连接是否禁用了自动提交?我如何设置大小?我对连接池非常陌生。你在哪里关闭语句(可能是连接)并将其返回到池中?我想我不是。我不知道如何使用池处理语句。你的连接池是否正好有8个连接?你的连接是否禁用了自动提交?我如何设置大小?我对连接池非常陌生。