最近进行编程的时候项目上线了,可是查询不了几次就发现出现连接池获取不到连接,我把连接池已经调到1000了,可是还是出现这种问题。所以怀疑我的数据库连接写的时候有问题,还请各位大神帮忙看看是怎么回事。
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class DBDAO {
public static Connection getCon(){
Connection conn=null;
try{
Context context = new InitialContext();
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
/*String url="jdbc:sqlserver://localhost:1433;DatabaseName=cx";
String user="sa";
String password="123456";
Connection conn= DriverManager.getConnection(url,user,password);*/
//获得数据源
Context envctx = (Context) context.lookup("java:comp/env");
DataSource ds = (DataSource) envctx.lookup("jdbc/DB");
//获取连接
conn = ds.getConnection();} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
//查询语句
public static ResultSet executeQuery(String sql) throws SQLException {
Connection con = getCon();
java.sql.Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
return rs;
}
public static ResultSet executeQuery(String sql, Object... obj) {
ResultSet rs =null;
try{
Connection con = getCon();
PreparedStatement pstmt = con.prepareStatement(sql);
for (int i = 0; i < obj.length; i++) {
pstmt.setObject(i + 1, obj[i]);
}
rs = pstmt.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
//执行增删改
public static ResultSet executeNonSelect(String sql) throws SQLException {
Connection con = getCon();
Statement stmt = con.createStatement();
return stmt.executeQuery(sql);
}
public static void executeNonReturn(String sql) {
Connection con=null;
Statement stmt=null;
try{
con= getCon();
stmt= con.createStatement();
stmt.execute(sql);
if(stmt!=null)stmt.close();
if(con!=null)con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(con!=null)
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}}
我调用的函数是这样写的
public static List getStudyCourse(String openid) {
// TODO Auto-generated method stub
ArrayList bkbs =new ArrayList();
if(null==openid)return null;
ResultSet rs=DBDAO.executeQuery("select * from buser,xsbkb where openid=? and xh=username and xksj='2016-03-01 00:00:00' ",openid);
try {
if(!rs.next())return null;
do{
Xsbkb bkb=new Xsbkb();
bkb.setXh(rs.getString("xh" +
""));
bkb.setXm(rs.getString("xm"));
bkb.setSjh(rs.getString("Sjh"));
bkb.setKcmc(rs.getString("kcmc"));
if(rs.getString("bz")!=null){
bkb.setBz(rs.getString("bz"));
}
bkbs.add(bkb);
}while(rs.next());
if(rs!=null) rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(rs!=null)
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("==================="+bkbs.size());
return bkbs;
}
我都关闭了连接 怎么还是出现获取不到连接呢
并没有看到你使用过的Connection被释放啊,你应该写一个释放的方法,查询过一次之后或者这个会话结束之后,关闭当前的数据库连接。你执行的方法里面
释放的是rs不是con。
你调用函数里没有close掉conn
建议提取Connection,PreparedStatement ,ResultSet为全局变量。
然后写一个close方法。判断是否为空,不为空就关闭,这样就可以了。
并没有关闭connection,,,
1:getCon方法逻辑完全错误,先自己建立数据连接,然后又从DataSource 中获取连接
2:连接池不是越大越好
3:有些地方没有关闭连接
4:没有事务
建议你使用spring