- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
如果我使用 DriverManager.getConnection()
和 DataSource.getConnection()
获取连接对象,当 .close()
是在那些对象上调用的?
在 .close()
方法调用之前,我从这两个不同的连接中获得了相关的 Statement
和 ResultSet
对象。在获得这两个对象后不久,如果我说 connection1.close()
(通过 DriverManager.getConnection()
),它将使连接对象无效,我不应该/允许访问相关的 Statement 和 ResultSet 对象。如果我错了,请纠正我?
第二种情况,现在如果我说 connection2.close()
(通过 DataSource.getConnection()
),它只是将它返回到池中。但连接仍然存在。我能否访问关联的 Statement
和 ResultSet
对象?
最佳答案
如果我们假设一个(基本的)DataSource
(即:一个不进行连接池的),那么您获得的物理连接与从 DriverManager< 获得的物理连接相同
(一些驱动程序甚至在内部使用来自 DataSource 的 DriverManager,或来自 DriverManager 的 DataSource)。因此这些连接的行为将相同。
现在,如果我们假设一个提供连接池的 DataSource
,那么 DataSource 本身使用一个 ConnectionPoolDataSource
(或类似的内部机制)来获得一个 PooledConnection
。此 PooledConnection 管理与数据库的实际物理连接。
当用户从 DataSource 请求连接时,DataSource 将 checkout PooledConnection,并向其请求 Connection
。然后 PooledConnection 将创建一个使用或包装物理连接的逻辑连接(例如使用代理)。 DataSource 会将逻辑连接返回给用户。
对于用户而言,逻辑连接在所有方面的行为都应与物理连接相同。因此,当用户关闭连接时,该逻辑连接和所有依赖的 JDBC 对象都将关闭,其行为与物理连接关闭相同。
JDBC 4.1 第 11.1 节说:
Connection pooling is completely transparent to the client: A client obtains a pooled connection and uses it just the same way it obtains and uses a non pooled connection.
和第 11.4 节:
If the application attempts to reuse the logical handle, the Connection implementation throws an SQLException.
和
For a given PooledConnection object, only the most recently produced logical Connection object will be valid. Any previously existing Connection object is automatically closed when the associated PooledConnection.getConnection method is called.
然而,在后台,当逻辑连接关闭时,PooledConnection 将向 DataSource 发出信号,表明它可以重用,然后 DataSource 会将其返回到连接池,或者关闭 PooledConnection(关闭物理连接) 如果它不再需要连接。
DataSource 还可以通过要求 PooledConnection 关闭逻辑连接来强制撤销来自用户的连接(例如,当连接 check out 时间过长时等)。
关于java - 连接行为 - DriverManager.getConnection() 和 DataSource.getConnection(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12541005/
我在 spring 上下文中初始化了数据源 bean。请问应该用什么方式?为什么我就是不能写 dataSource.getConnection()? 最佳答案 有一个重要区别:dataSource.g
如果我使用 DriverManager.getConnection() 和 DataSource.getConnection() 获取连接对象,当 .close() 是在那些对象上调用的? 在 .cl
在 DataSource 接口(interface)上,我发现了两种获取连接的方法,有和没有用户名和密码参数。 Connection getConnection()Connection getConn
我想对这段代码进行单元测试,以验证调用了哪个 DriverManager.getConnection() 。DriverManager.getConnection(url,user,pass) 或 D
我正在使用jsPlumb statemachine 。我想稍后保存并加载我的图表。我用谷歌搜索,发现我必须使用 json 对象来做到这一点。我尝试了几个例子stackoverflow 。我能够获得每个
如何模拟 DriverManager.getConnection() 方法? 我想测试我的方法 setUpConnectiontoDB() 我用 PowerMock、easyMock 和 Mokito
数据库连接如下所示 public Connection getDBConection(){ Context context = new InitialContext(); DataSo
我在 Java 文件中找到了这个方法,我想知道这里发生了什么?这真的有效吗?我认为这个方法什么都不做,因为 getConnection() 方法创建了一个新连接,然后它被关闭了。我说得对吗? publ
我正在尝试从nodejs中的mysql连接池获取连接。但是池的 getConnection 方法不会返回回调函数。 (因此它不会显示任何错误或返回连接)。但我的 mysql 工作台显示已建立连接。当我
我正在使用 MySQL 5.5 及其默认值。我创建了一个用户/密码并运行了一个脚本来创建一个名为员工的数据库。通过命令提示符我可以访问数据库: mysql -u user -p SELECT * FR
我有以下情况:我有一个与数据库 (MySQL) 连接的 java 代码。当我从 eclipse 运行 java 代码并从数据库中获取数据时,一切正常。但是当我从 html 代码运行 java 代码作为
我的工作电脑有一个奇怪的问题,也许你可以解决问题。我目前正在使用 jee 进行开发,我遇到了有关 MySQL DB 的问题。 假设我有这个: for(int i = 0; i<20; i++) { /
我编写了 DAO 类 public List getAllClassRooms() { List classRoomsList = new ArrayList<>(); String
在使用 BoneCP 连接池时,我遇到了以下困惑,并希望听到一些关于这方面的建议: 是 getConnection BoneCP线程安全的方法?当有许多线程请求并行连接时,使用它的最佳方法是什么? 每
最近,我们在Hive连接中添加了SSL。我们通过添加 ssl=1;sslTrustStore=C:\\keytabs\\keys.truststore;trustStorePassword=foo 到
在 SignalR 中,ITransportHeartbeat.GetConnections() 的实现应该会为我提供正在跟踪的连接列表。在我的一个案例中,我总是得到一个不再存在的网络套接字连接。 以
我正在使用jdbc连接池,如果我编写像这样的代码 DatabaseMetaData dbMeta = getConnection().getMetaData(); 其中 getConnection()
我有以下代码来测试新服务器上访问数据库的链接,一切都适用于现有服务器,并且我能够访问该文件夹。 !if.exists 返回 true,我可以使用 Runtime.getRuntime().exec("
我正在使用 Java 和 mysql 作为数据库,但遇到了一个奇怪的问题:我的一位客户的连接非常不稳定,有时丢包率可能很高。好吧,我知道这不是软件的错误,但我去那里进行了测试,当程序调用“Driver
这个问题已经有答案了: Solving a "communications link failure" with JDBC and MySQL [duplicate] (25 个回答) 已关闭 4 年
我是一名优秀的程序员,十分优秀!