[英]Cannot close a connection while a transaction is still alive Exception on connection.close()
I have a method that creates a Connection with Embedded Derby Database and performs a Select query on it.我有一种方法可以创建与嵌入式 Derby 数据库的连接并对其执行 Select 查询。
public PersonMID findPersonMID(String personAlias, CodeUID aliasTypeCodeUID, LogicalDomainMID logicalDomainMID) throws SQLException
{
Connection connection = getConnection();
try
{
QueryExecutor<Long> findPersonIdByPersonAliasExecutor = FindPersonIdByPersonAliasDelegate.getExecutor(authority,connection, personAlias);
Long result = findPersonIdByPersonAliasExecutor.execute();
if(result == null)
{
return null;
}
return PersonMID.create(authority, result);
}
finally
{
JDBCAssistant.close(connection);
}
Here is what my query looks like:这是我的查询的样子:
select P.PRSON_ID from PRSON_ALIAS PA join PRSON P on P.PRSON_ID = PA.PRSON_ID and P.LOGICAL_DOMAIN_ID = ? where PA.PRSON_ALIAS_TYPE_CD = ? and PA.ALIAS = ?
When I run through this code, I get an Exception JDBCException: java.sql.SQLException: Cannot close a connection while a transaction is still active.
当我运行这段代码时,我得到一个异常
JDBCException: java.sql.SQLException: Cannot close a connection while a transaction is still active.
But when I call Connection.commit()
before I close the Connection
or set autoCommit
true
(it is set to false
by default for my Connection
) for my Connection
, it is allowing me to successfully close the connection and get the reqired result.但是,当我打电话
Connection.commit()
之前,我关闭Connection
或设置autoCommit
true
(它被设置为false
默认为我的Connection
)对我的Connection
,它让我成功地关闭连接,并获得reqired结果。
But do I really need to call commit()
for a Select
operation?但是我真的需要为
Select
操作调用commit()
吗? What's there to commit?有什么好犯的? Is there a lock somewhere that is not being released if I dont commit?
如果我不提交,是否有一个不会被释放的锁?
This post says I shouldn't have to do it.这篇文章说我不应该这样做。 I should be able to close my connection without having to commit or rollback.
我应该能够关闭我的连接而不必提交或回滚。
What am I missing?我错过了什么?
If you are not in autoCommit
mode, then a SELECT
statement is in fact holding read locks, depending on your isolation level, and so committing a SELECT
query is more than a no-op.如果您未处于自动
autoCommit
模式,则SELECT
语句实际上持有读锁,具体取决于您的隔离级别,因此提交SELECT
查询不仅仅是无操作。
Yes, there is no change to the database, but the commit still tells the database engine that your transaction is finished looking at the data, and it can therefore allow other transactions to modify the data.是的,数据库没有变化,但提交仍然告诉数据库引擎您的事务已完成查看数据,因此它可以允许其他事务修改数据。
Here's some background material:这是一些背景材料:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.