Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to enlist connection in transaction: enlistResource returns 'false'

All of sudden I got the following error continuously for a select query.

Unable to enlist connection in transaction: enlistResource returns 'false'

This appears to be thrown from DBCP (https://commons.apache.org/proper/commons-dbcp/jacoco/org.apache.commons.dbcp2.managed/TransactionContext.java.html)

Essentially, this code calls javax.transaction.Transaction.enlistResource

Looking at the API, it says "Enlist the resource specified with the transaction associated with the target Transaction object. true if the resource was enlisted successfully; otherwise false."

I am still not clear why this exception thrown.

  1. What is enlist the resource to transaction?
  2. When enlist resource returns false? I mean when transaction will not be able to enlist the resource?
  3. How to avoid this problem?

Tech Stack:

  • TomEE 7.0.4
  • JPA
  • MariaDB

Data source config:

<Resource id="jdbc/myDS" type="javax.sql.DataSource">
    dataSourceCreator = tomcat
    jtaManaged        = true
    driverClassName   = ${jdbc.driver}
    url               = ${jdbc.url}
    username          = ${jdbc.username}
    password          = ${jdbc.password}
    initialSize       = 5
    maxActive         = 100
    maxIdle           = 10
    minIdle           = 5
    maxWait           = 30000
    validationQuery   = SELECT 1
    testOnBorrow      = false
    testOnReturn      = false
    testWhileIdle     = true
    timeBetweenEvictionRunsMillis = 1800000
    numTestsPerEvictionRun = 2
    minEvictableIdleTimeMillis = 1800000
    accessToUnderlyingConnectionAllowed = false
</Resource>

Stack Trace:

Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: Unable to enlist connection in transaction: enlistResource returns 'false'. 
    at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:218) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:198) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.access$000(LoggingConnectionDecorator.java:58) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator$LoggingConnection.prepareStatement(LoggingConnectionDecorator.java:250) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.lib.jdbc.DelegatingConnection.prepareStatement(DelegatingConnection.java:133) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.lib.jdbc.ConfiguringConnectionDecorator$ConfiguringConnection.prepareStatement(ConfiguringConnectionDecorator.java:139) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.lib.jdbc.DelegatingConnection.prepareStatement(DelegatingConnection.java:133) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.jdbc.kernel.JDBCStoreManager$RefCountConnection.prepareStatement(JDBCStoreManager.java:1642) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.lib.jdbc.DelegatingConnection.prepareStatement(DelegatingConnection.java:122) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.jdbc.sql.SQLBuffer.prepareStatement(SQLBuffer.java:513) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.jdbc.sql.SQLBuffer.prepareStatement(SQLBuffer.java:493) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.jdbc.sql.SelectImpl.prepareStatement(SelectImpl.java:480) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.jdbc.sql.SelectImpl.execute(SelectImpl.java:421) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.jdbc.sql.SelectImpl.execute(SelectImpl.java:392) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.jdbc.sql.LogicalUnion$UnionSelect.execute(LogicalUnion.java:427) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.jdbc.sql.LogicalUnion.execute(LogicalUnion.java:230) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.jdbc.sql.LogicalUnion.execute(LogicalUnion.java:220) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.jdbc.kernel.SelectResultObjectProvider.open(SelectResultObjectProvider.java:93) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.kernel.QueryImpl$PackingResultObjectProvider.open(QueryImpl.java:2075) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.lib.rop.EagerResultList.<init>(EagerResultList.java:33) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.kernel.QueryImpl.toResult(QueryImpl.java:1257) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:1013) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:869) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:800) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.kernel.DelegatingQuery.execute(DelegatingQuery.java:541) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.persistence.QueryImpl.execute(QueryImpl.java:274) ~[openjpa-2.4.2.jar:2.4.2]
    at org.apache.openjpa.persistence.QueryImpl.getResultList(QueryImpl.java:290) ~[openjpa-2.4.2.jar:2.4.2]
like image 253
yottabrain Avatar asked Jan 29 '18 04:01

yottabrain


1 Answers

Just had the same issue and digged a little more into my log file. Right before the first appearance of

enlistResource returns 'false'

I found a

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Checking further up and found a

WARNING [https-jsse-nio-8443-exec-1] org.apache.geronimo.transaction.manager.TransactionImpl.enlistResource Unable to enlist XAResource org.apache.openejb.resource.jdbc.managed.local.LocalXAResource@2ce72e5a, errorCode: 0
    javax.transaction.xa.XAException: Count not turn off auto commit for a XA transaction

All this leaded me to a old post from tomee-openejb mailing list. So my recent tomee.xml configuration looked like that:

<Resource id="[Resourcename]" type="javax.sql.DataSource">
    jdbcDriver = com.mysql.jdbc.Driver
    jdbcUrl = jdbc:mysql://localhost:3306/[Databasename]
    userName = [Username]
    password = [Password]
</Resource>

Now I changed it to the following config:

<Resource id=[Name] type="javax.sql.DataSource">
    jdbcDriver = com.mysql.jdbc.Driver
    jdbcUrl = jdbc:mysql://localhost:3306/[Databasename]
    jtaManaged = true
    username = [Username]
    password = [Password]
    defaultAutoCommit = false
    testOnReturn = true
    testWhileIdle = true
    timeBetweenEvictionRunsMillis = 60
    initialSize = 2
    minIdle = 2
    validationQuery = "select 1" 
</Resource>

Finger crossed that it will work without any further issue. If I don't update here anymore it will just have worked fine.


Further information:

  • Tomee DataSource Configuration
like image 132
wittich Avatar answered Nov 14 '22 19:11

wittich