简体   繁体   English

事务仍处于活动状态时无法关闭连接 connection.close() 上的异常

[英]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:这是一些背景材料:

  1. Isolation levels and concurrency;隔离级别和并发性; https://db.apache.org/derby/docs/10.12/devguide/cdevconcepts15366.html https://db.apache.org/derby/docs/10.12/devguide/cdevconcepts15366.html
  2. Shared locks: https://db.apache.org/derby/docs/10.12/devguide/cdevconcepts842304.html共享锁: https : //db.apache.org/derby/docs/10.12/devguide/cdevconcepts842304.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM