Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring: HibernateTransactionManager handling multiple datasources
    primarykey
    data
    text
    <p>In the following piece of code (Spring 3):</p> <pre><code>@Transactional("txManager") public class DaoHolder { @Transactional(value="txManager", readOnly=false, propagation=Propagation.REQUIRES_NEW, rollbackFor={Exception.class}) private void runTransactionalMethod() throws Exception { dao1.insertRow(); dao2.insertRow(); //throw new Exception(); } //... } </code></pre> <ul> <li>dao1 uses a session factory attached to datasource1</li> <li>dao2 uses a session factory attached to datasource2</li> <li>txManager is a <strong>HibernateTransactionManager</strong> using the same session factory as dao1 </li> </ul> <p>The code above <strong>works correctly in a transactional manner</strong> - in particular, when no exception is thrown, each dao operation gets committed (to 2 different datasources). When an exception is thrown each dao operation gets rolled back.</p> <p>My question is: <strong>why does it work?</strong> Everywhere I've read I've been told to use a JtaTransactionManager when handling multiple datasources. I'd prefer not to use JTA. <strong>What might be the consequences if I leave it running under a HibernateTransactionManager?</strong></p> <hr> <hr> <p>Some more details for the interested:</p> <p>Each datasource is defined like so:</p> <pre><code>&lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt; &lt;property name="driverClassName" value="${jdbc.driverClassName}" /&gt; &lt;property name="url" value="${jdbc.url}" /&gt; &lt;property name="username" value="${jdbc.username}" /&gt; &lt;property name="password" value="${jdbc.password}" /&gt; &lt;property name="initialSize" value="${jdbc.initial_size}" /&gt; &lt;property name="maxActive" value="${jdbc.max_active}" /&gt; &lt;/bean&gt; </code></pre> <p>Each session factory is defined like so:</p> <pre><code>&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;property name="mappingResources"&gt; &lt;list&gt; ... multiple *.hbm.xml files here ... &lt;/list&gt; &lt;/property&gt; &lt;property name="hibernateProperties"&gt; &lt;props&gt; &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt; &lt;prop key="hibernate.show_sql"&gt;${hibernate.show_sql}&lt;/prop&gt; &lt;/props&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>The transaction manager is defined like so:</p> <pre><code>&lt;bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt; &lt;property name="sessionFactory" ref="sessionFactory"/&gt; &lt;/bean&gt; </code></pre> <p>Each dao class extends HibernateDaoSupport and the content of the insertRow method is more or less like so for dao1:</p> <pre><code>getHibernateTemplate().save(obj); </code></pre> <p>and for dao2:</p> <pre><code>getHibernateTemplate().merge(obj); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload