[英]Why won't my Java JDBC to Firebird database connection disconnect?
I am using the following code to connect to a firebird database 我正在使用以下代码连接到Firebird数据库
public static Connection dbStatic;
...
public void getConnection(){
FBWrappingDataSource DataSource = new FBWrappingDataSource();
DataSource.setDatabase("localhost/3050:C:/MyDatabase.FDB");
DataSource.setDescription("TNS Development Database");
DataSource.setType("TYPE4");
DataSource.setEncoding("ISO8859_1");
DataSource.setLoginTimeout(10);
try {
dbStatic = DataSource.getConnection("UserName", "Password");
} catch (SQLException e) {
e.printStackTrace();
}
}
...and the following to disconnect: ...,然后断开连接:
...
dbStatic.close();
...
I am using Firebird 2.1 runing on a Windows 7-32 bit machine, with Java verstion 1.7, Jaybird version 2.2.8, Tomcat version 7.xx running on Win7-32bit, Browser is Chrome version something or other (newish) running Win XP SP3. 我正在运行在Windows 7-32位计算机上运行的Firebird 2.1,并且Java版本1.7,Jaybird版本2.2.8,Tomcat版本7.xx在Win7-32位上运行,浏览器是Chrome版本或运行Win XP的其他(新版本) SP3。
I use a third party tool called IBExpert to look at the number of connections and/or I run this statement: 我使用名为IBExpert的第三方工具查看连接数和/或运行以下语句:
select * from mon$attachments;
When I look at the number of connections to the database after the .close() statement runs the number does not decrease. 当我在.close()语句运行后查看与数据库的连接数时,该数目并没有减少。 Why is that?
这是为什么? The number of connections do decrease if I wait long enough, or if the Tomcat server is restarted.
如果等待足够长的时间或重新启动Tomcat服务器,连接数的确会减少。 Closing the browser does not affect the connections.
关闭浏览器不会影响连接。
As Andreas already pointed out in the comments, FBWrappingDataSource
is a connection pool. 正如Andreas在评论中所指出的那样,
FBWrappingDataSource
是一个连接池。 This means that the pool keeps physical connections open, and it hands out logical connections backed by the physical connections in the connection pool. 这意味着池使物理连接保持打开状态,并分发由连接池中的物理连接支持的逻辑连接。 Once you call
close()
on that logical connection, the physical connection is returned to the pool as available for reuse. 在该逻辑连接上调用
close()
,物理连接将返回到池中,以供重新使用。 The physical connection remains open. 物理连接保持打开状态。
If you want to close all connections, you need to call FBWrappingDataSource.shutdown()
. 如果要关闭所有连接,则需要调用
FBWrappingDataSource.shutdown()
。 This closes all physical connections that are not currently in use(!), and marks the data source as shutdown. 这将关闭当前未使用的所有物理连接(!),并将数据源标记为已关闭。
However, everything in package org.firebirdsql.pool
should be considered deprecated; 但是,软件包
org.firebirdsql.pool
所有内容都应视为已弃用; it will be removed in Jaybird 3. See Important changes to Datasources 它将在Jaybird 3中删除。请参阅对数据源的重要更改
If you just want a data source, use org.firebirdsql.pool.FBSimpleDataSource
(with Jaybird 3 you will need to use org.firebirdsql.ds.FBSimpleDataSource
instead). 如果只需要数据源,请使用
org.firebirdsql.pool.FBSimpleDataSource
(对于Jaybird 3,您将需要使用org.firebirdsql.ds.FBSimpleDataSource
代替)。
If you want connection pooling, use a third party connection pool library like HikariCP, DBCP or c3p0. 如果要连接池,请使用第三方连接池库,例如HikariCP,DBCP或c3p0。
That said, I want to point out several things you should consider: 也就是说,我想指出您应该考虑的几件事:
DriverManager
to create the connection. DriverManager
来创建连接会更简单。 DataSource
should be called dataSource
if you follow the common Java conventions DataSource
应该称为dataSource
setLoginTimeout(Integer.parseInt(10))
should lead to a compilation error, as there is no method Integer.parseInt
that takes an int
, and the method itself already accepts an int
. setLoginTimeout(Integer.parseInt(10))
应该导致编译错误,因为没有方法Integer.parseInt
接受一个int
,并且该方法本身已经接受了int
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.