[英]Connection.close() does not closes the connection, it make it sleep forever?
在我用 java 編寫並使用 tomcat 和 sql server 的 Web 應用程序中,我無法通過鍵入 connection.close() 來關閉數據庫連接。 當我將 sp_who 寫入 SSMS 時,我可以看到當我的應用程序執行 sql 操作時,我打開的睡眠連接數會增加。
示例代碼如下:
BaseRepository baseRepository = new BaseRepository();
try{
baseRepository.createStatement();
baseRepository.stmt.executeUpdate("update AutoRunURLs set STATUS = 0");
}catch (SQLException e){
e.printStackTrace();
}finally {
baseRepository.cleanResources();
}
這是我上面使用的其他功能:
public void openConnection() {
try {
this.conn = ds.getConnection(); // ds is an instance of javax.sql.DataSource
this.isOpen = true;
} catch (Exception e) {
e.printStackTrace();
}
}
public void createStatement() {
try {
if (!this.isOpen) this.openConnection();
this.stmt = this.conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
public void cleanResources() {
try {
if (this.rs != null) {
rs.close();
this.rs = null;
}
if (this.stmt != null) {
stmt.close();
this.stmt = null;
}
if (this.conn != null) {
this.closeConnection();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (this.conn != null) this.closeConnection();
}
}
public void closeConnection() {
try {
if (this.conn != null){
this.conn.close();
}
this.isOpen = false;
} catch (Exception e) {
e.printStackTrace();
}
}
當我運行上面以BaseRepository baseRepository ...
開頭的第一部分時BaseRepository baseRepository ...
發生了一個睡眠連接,我在鍵入 sp_who 時看到它並沒有關閉(我等了大約一天)。 這是為什么? 我怎樣才能防止這種情況?
我又遇到了一種情況。 在我的 tomcat 配置中,我將“maxIdle”值設置為 10,但即使是睡眠連接在一周后也會增加到數千。 為什么 maxIdle 不起作用? 這是我如何設置它:
<Context>
<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<Resource auth="Container" driverClassName="net.sourceforge.jtds.jdbc.Driver" maxTotal="999999" maxIdle="10" "Database info here..." validationQuery="select 1"/>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
我該如何解決這些問題? 問候,
編輯:我實際上是通過創建一個計划任務來管理它,該任務每 x 分鍾運行一次並殺死睡眠超過 y 分鍾的睡眠連接。 但這不是我想要的方式。 任何其他解決方案都會很棒。
您正在使用 Tomcat 內置的連接池。 這意味着它保持連接打開以供重用。 當您關閉代碼中的連接時,它們實際上並未關閉,而是返回到池中供您的應用程序重新使用。 這提高了效率,因為打開新連接需要時間。
換句話說,沒有任何問題,保持打開的連接是預期的行為。
不過,您可能希望將maxTotal
降低到更合理的值,例如 10 - 20 而不是 999999。
順便說一句,您處理連接的方式有點奇怪,並且很容易泄漏連接。 您可能只想在真正需要時從數據源獲取連接,並了解 try-with-resources,以便盡快關閉它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.