title: "Local transaction already has 1 non-XA Resource: cannot add more resources" error
body: <p>After reading previous questions about this error, it seems like all of them conclude that you need to enable XA on all of the data sources. But:</p>
  
  <ol>
  <li>What if I don't want a distributed
  transaction? What would I do if I want to
  start transactions on two different 
  databases at the same time, but
  commit the transaction on one database
  and roll back the transaction on
  the other? </li>
  <li>I'm wondering how my code
  actually initiated a distributed
  transaction. It looks to me like I'm
  starting completely separate
  transactions on each of the
  databases.</li>
  </ol>
  
  <p><strong>Info about the application:</strong></p>
  
  <p>The application is an EJB running on a Sun Java Application Server 9.1</p>
  
  <p>I use something like the following spring context to set up the hibernate session factories:</p>
  
  <pre><code>&lt;bean id="dbADatasource" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
      &lt;property name="jndiName" value="jdbc/dbA"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dbASessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dbADatasource" /&gt;
      &lt;property name="hibernateProperties"&gt;
          hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
          hibernate.default_schema=schemaA
      &lt;/property&gt;
      &lt;property name="mappingResources"&gt;
          [mapping resources...]
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dbBDatasource" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
      &lt;property name="jndiName" value="jdbc/dbB"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dbBSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dbBDatasource" /&gt;
      &lt;property name="hibernateProperties"&gt;
          hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
          hibernate.default_schema=schemaB
      &lt;/property&gt;
      &lt;property name="mappingResources"&gt;
          [mapping resources...]
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Both of the JNDI resources are javax.sql.ConnectionPoolDatasoure's. They actually both point to the same connection pool, but we have two different JNDI resources because there's the possibility that the two, completely separate, groups of tables will move to different databases in the future.</p>
  
  <p>Then in code, I do:</p>
  
  <pre><code>sessionA = dbASessionFactory.openSession();
  sessionB = dbBSessionFactory.openSession();
  sessionA.beginTransaction();
  sessionB.beginTransaction();
  </code></pre>
  
  <p>The sessionB.beginTransaction() line produces the error in the title of this post - sometimes. I ran the app on two different sun application servers. On one runs it fine, the other throws the error. I don't see any difference in how the two servers are configured although they do connect to different, but equivalent databases.</p>
  
  <p><strong>So the question is</strong></p>
  
  <ol>
  <li>Why doesn't the above code start
  completely independent transactions?</li>
  <li>How can I force it to start
  independent transactions rather than
  a distributed transaction?</li>
  <li>What configuration could cause the difference in
  behavior between the two application
  servers?</li>
  </ol>
  
  <p>Thanks.</p>
  
  <p>P.S. the stack trace is:</p>
  
  <pre><code>Local transaction already has 1 non-XA Resource: cannot add more resources. 
  at com.sun.enterprise.distributedtx.J2EETransactionManagerOpt.enlistResource(J2EETransactionManagerOpt.java:124) 
  at com.sun.enterprise.resource.ResourceManagerImpl.registerResource(ResourceManagerImpl.java:144) 
  at com.sun.enterprise.resource.ResourceManagerImpl.enlistResource(ResourceManagerImpl.java:102) 
  at com.sun.enterprise.resource.PoolManagerImpl.getResource(PoolManagerImpl.java:216) 
  at com.sun.enterprise.connectors.ConnectionManagerImpl.internalGetConnection(ConnectionManagerImpl.java:327) 
  at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:189) 
  at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:165) 
  at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:158) 
  at com.sun.gjc.spi.base.DataSource.getConnection(DataSource.java:108) 
  at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:82) 
  at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446) 
  at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167) 
  at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142) 
  at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85) 
  at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1354) 
  at [application code ...]
  </code></pre>