public class TConnection {
private static Connection conn;
public static Connection getConnection(){
// 加载驱动
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
// 创建连接
conn = DriverManager.getConnection("jdbc:mysql://192.168.1.3:3306/data","root","root");
}catch (Exception e) {
conn=null;
MessageDao md = new MessageDao();
//发送异常消息,通知前台
if(! md.dbExceptionMessageAgain())
md.sendMessage(null, 42);
md = null;
}
//返回连接
return conn;
}
public static void closeConnection(ResultSet rs,PreparedStatement pst,Connection conn) {
try {
if(rs!=null) rs.close();
if(pst!=null) pst.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
rs = null;
pst = null;
conn = null;
}
}
public static void closeConnection(Connection conn) {
try {
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
conn = null;
}
}
}//应用
public void ways1(){
Connection connT=null;
connT = TConnection.getConnection();
if(connT==null){
request.setAttribute("error", "服务器繁忙,请稍后再试.");
ConnFactory.close(connP);
return;
}
...
TConnection.close(connT);
}
现在请教一下大家,我连接的数据库最大连接数是200,为什么在服务器连接这里连续异常之后就再连不上了呢?
异常好像是连接已满.不过也不一定,日志全丢了.
这个连接用的比较频繁.不过也不会到并发100以上.
另外,这个已经无法再用连接池,因为程序已经连了2个,之前用了池,会出错误.
忘高手相助.
private static Connection conn;
public static Connection getConnection(){
// 加载驱动
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
// 创建连接
conn = DriverManager.getConnection("jdbc:mysql://192.168.1.3:3306/data","root","root");
}catch (Exception e) {
conn=null;
MessageDao md = new MessageDao();
//发送异常消息,通知前台
if(! md.dbExceptionMessageAgain())
md.sendMessage(null, 42);
md = null;
}
//返回连接
return conn;
}
public static void closeConnection(ResultSet rs,PreparedStatement pst,Connection conn) {
try {
if(rs!=null) rs.close();
if(pst!=null) pst.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
rs = null;
pst = null;
conn = null;
}
}
public static void closeConnection(Connection conn) {
try {
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
conn = null;
}
}
}//应用
public void ways1(){
Connection connT=null;
connT = TConnection.getConnection();
if(connT==null){
request.setAttribute("error", "服务器繁忙,请稍后再试.");
ConnFactory.close(connP);
return;
}
...
TConnection.close(connT);
}
现在请教一下大家,我连接的数据库最大连接数是200,为什么在服务器连接这里连续异常之后就再连不上了呢?
异常好像是连接已满.不过也不一定,日志全丢了.
这个连接用的比较频繁.不过也不会到并发100以上.
另外,这个已经无法再用连接池,因为程序已经连了2个,之前用了池,会出错误.
忘高手相助.
public void ways1(){
Connection connT=null;
connT = TConnection.getConnection();
if(connT==null){
request.setAttribute("error", "服务器繁忙,请稍后再试.");
ConnFactory.close(connP);
return;
}
...
TConnection.close(connT);
}
这句ConnFactory.close(connP); 是何意思啊?
try {
if(rs!=null) rs.close();
if(pst!=null) pst.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
rs = null;
pst = null;
conn = null;
}
}
还是finally里面关闭 connection 吧,确保 connection 真的被关闭了
不好意思ConnFactory.close(connP); 复制错了
应该是TConnection.closeConnection(conn);
或者有没有可能狂抛连接失败的异常导致jdk堆空间不足?