我正在使用以下代码段连接数据库
private Connection getConnection(JspWriter out, String host, String dbname,
String port, String user, String pwd) {
try {
if (host == null || dbname == null) {
throw new IllegalArgumentException("Invalid host or dbname");
}
String url = "jdbc:mysql://" + host + ":" + port + "/" + dbname;
String clazz = "org.gjt.mm.mysql.Driver";
Driver driver = (Driver) Class.forName(clazz).newInstance();
DriverManager.registerDriver(driver);
return DriverManager.getConnection(url, user, pwd);
} catch (Exception e) {
System.out.println("Exception occured while get connection :"+ e);
}
return null;
}
执行查询后,我正在关闭连接对象
private JSONArray executeQuery(JspWriter out, Connection connection,String query, Object[] params) throws Exception {
PreparedStatement st = null;
ResultSet rs = null;
try {
//connection = getConnection();
st = connection.prepareStatement(query);
int c = 1;
for (Object param : params) {
st.setObject(c++, param);
}
rs = st.executeQuery();
if (rs == null) {
return null;
}
ResultSetMetaData meta = rs.getMetaData();
List<String> columns = getColumnNames(meta);
JSONArray json = new JSONArray();
while (rs.next()) {
JSONObject obj = new JSONObject();
for (String column : columns) {
obj.put(column, rs.getObject(column));
}
json.put(obj);
}
return json;
} catch (Exception e) {
System.out.println("Exception occurred while execute query" + e);
} finally {
try {
if (rs != null) {
rs.close();
}
} catch(Exception ex) {
//Print exception
}
try {
if (st != null) {
st.close();
}
} catch(Exception ex) {
//Print exception
}
try {
if(connection != null) {
connection.close();
}
if(connection.isClosed()) {
connection = null;
}
} catch(Exception ex) {
//Print exception
}
}
return null;
}
虽然我已使用.close()
方法关闭了连接。我确保用connection.closed()
关闭连接并返回true - 表示连接已成功关闭。
一旦连接限制达到最大值 - 我得到DB Connection Exhausted
并且我的服务器无法进一步连接。
有人可以帮助我进一步调试吗。
答案 0 :(得分:2)
开始使用Java 7中引入的try-with-resources syntax:
// This logic opens the connection by calling getConnection()
// It must also close the connection again, e.g. with try-with-resources
try (Connection connection = getConnection()) {
doSomething(connection);
}
...
// This logic didn't open a connection. It receives it from someone else
// It mustn't close the connection!
void doSomething(Connection connection) {
// But if other resources are opened, then they must be closed here:
try (Statement stmt = connection.createStatement()) {
doSomething(stmt);
}
}
..
// Again, this method didn't open the statement, it shouldn't close it
void doSomething(Statement statement) {
..
}
它更方便,更不容易出错。
关闭连接的责任始终是打开它的逻辑。如果没有获得连接,您的方法就不应该关闭连接。来电者应该(你的逻辑没有发布)。理想情况下,您无论如何都要使用连接池,但这可能不在此处。
一个例子:
Sub Filter()
Application.ScreenUpdating = False
Dim x As Range
Dim rng As Range
Dim last As Long
Dim sht As String
'specify sheet name in which the data is stored
sht = "Filter This"
'change filter column in the following code
last = Sheets(sht).Cells(Rows.Count, "C").End(xlUp).Row
Set rng = Sheets(sht).Range("A1:H" & last)
Sheets(sht).Range("C1:C" & last).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Range("AA1"), _
Unique:=True
For Each x In Range([AA2], Cells(Rows.Count, "AA").End(xlUp))
With rng
.AutoFilter
.AutoFilter Field:=3, Criteria1:=x.Value
.SpecialCells(xlCellTypeVisible).Copy
Sheets.Add(After:=Sheets(Sheets.Count)).Name = x.Value
ActiveSheet.Paste
End With
Next x
'Turn off filter
Sheets(sht).AutoFilterMode = False
With Application
.ScreenUpdating = True
.CutCopyMode = False
End With
End Sub