title: Atomikos / Spring - Global Transaction over two DBs
body: <p>I am using Spring and trying to setup a global transaction spanning over two MS SQL Server DBs. The app is running inside Tomcat 6.</p>
  <p>I have these definitions:</p>
  <pre><code>&lt;bean id=&quot;dataSource1&quot; class=&quot;org.apache.commons.dbcp.BasicDataSource&quot; destroy-method=&quot;close&quot;&gt;
  ....
  &lt;/bean&gt;
  
  &lt;bean id=&quot;sessionFactory1&quot;
        class=&quot;org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean&quot;&gt;
      &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource1&quot;/&gt;
  ....
  &lt;/bean&gt;
  
  &lt;bean id=&quot;hibernateTransactionManager1&quot;
        class=&quot;org.springframework.orm.hibernate3.HibernateTransactionManager&quot;&gt;
      &lt;property name=&quot;sessionFactory&quot;&gt;
          &lt;ref local=&quot;sessionFactory1&quot;/&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  
  &lt;bean id=&quot;dataSource2&quot; class=&quot;org.apache.commons.dbcp.BasicDataSource&quot; destroy-method=&quot;close&quot;&gt;
  ....
  &lt;/bean&gt;
  
  &lt;bean id=&quot;sessionFactory2&quot;
        class=&quot;org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean&quot;&gt;
      &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource2&quot;/&gt;
  ....
  &lt;/bean&gt;
  
  &lt;bean id=&quot;hibernateTransactionManager2&quot;
        class=&quot;org.springframework.orm.hibernate3.HibernateTransactionManager&quot;&gt;
      &lt;property name=&quot;sessionFactory&quot;&gt;
          &lt;ref local=&quot;sessionFactory2&quot;/&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  <p>Then also, each DAO is linked either to <code>sessionFactory1</code> or to <code>sessionFactory2</code>.</p>
  <pre><code>&lt;bean name=&quot;stateHibernateDao&quot; class=&quot;com.project.dao.StateHibernateDao&quot;&gt;
        &lt;property name=&quot;sessionFactory&quot; ref=&quot;sessionFactory1&quot;/&gt;
  &lt;/bean&gt;
  </code></pre>
  <p>Also, I recently added these two.</p>
  <pre><code>&lt;bean id=&quot;atomikosTransactionManager&quot; class=&quot;com.atomikos.icatch.jta.UserTransactionManager&quot; init-method=&quot;init&quot; destroy-method=&quot;close&quot;&gt; 
          &lt;property name=&quot;forceShutdown&quot; value=&quot;false&quot; /&gt;
          &lt;property name=&quot;transactionTimeout&quot; value=&quot;300&quot; /&gt;
  &lt;/bean&gt;
  
  &lt;bean id=&quot;atomikosUserTransaction&quot; class=&quot;com.atomikos.icatch.jta.UserTransactionImp&quot;&gt; 
     &lt;property name=&quot;transactionTimeout&quot; value=&quot;300&quot; /&gt; 
  &lt;/bean&gt; 
  </code></pre>
  <p>I am trying to programmatically manage the global transaction
  (this is some old legacy code and I don't want to change it too
  much so I prefer keeping this managed programmatically).</p>
  <p>So now I have this <code>UserTransaction ut</code> (injected from Spring), so I call <code>ut.begin()</code>, do some DB/DAO operations to the two DBs through the DAOs, then I call <code>ut.commit()</code>.</p>
  <p>The thing is that even before the <code>ut.commit()</code> call, I can see the data is already committed to the DBs?!</p>
  <ol>
  <li><p>I don't think Atomikos is aware of my two DBs, their data sources, session factories, etc. I don't think it starts any transactions on them. Looks like they are not enlisted at all in the global transaction.</p>
  </li>
  <li><p>To me it seems that each DB/DAO operation goes to the SQL Server on its own, so SQL Server creates an implicit transaction for just that DAO/DB operation, applies the operation and commits the implicit the transaction.</p>
  </li>
  </ol>
  <p>But 1) and 2) are just guesses of mine.</p>
  <p>My questions:</p>
  <ol>
  <li><p>Do I need to start the two DB transactions myself (but OK, this is what I am currently doing and I am trying to get rid of; that's why I am trying to use Atomikos to start with)?</p>
  </li>
  <li><p>How I can configure all this correctly so that when I call <code>ut.begin()</code> it begins a global transaction to the two DBs and when I call <code>ut.commit()</code> it commits it?</p>
  </li>
  <li><p>I haven't played with JTA recently so seems to me I am missing something quite basic here. What is it?</p>
  </li>
  </ol>
  <h2>Edit 1</h2>
  <pre><code>&lt;bean id=&quot;globalTransactionManager&quot; class=&quot;org.springframework.transaction.jta.JtaTransactionManager&quot;&gt;
      &lt;property name=&quot;userTransaction&quot; ref=&quot;atomikosUserTransaction&quot;/&gt;
      &lt;property name=&quot;transactionManager&quot; ref=&quot;atomikosTransactionManager&quot; /&gt;
      &lt;property name=&quot;allowCustomIsolationLevels&quot; value=&quot;true&quot; /&gt;
      &lt;property name=&quot;transactionSynchronization&quot; value=&quot;2&quot; /&gt;
  &lt;/bean&gt;
  </code></pre>