Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I assume you have a set of DAOs that should use <code>dataSource1</code> and appropriate <code>sessionFactory1</code>, while others should use different <code>dataSouce2</code> and <code>sessionFactory2</code> based on <code>dataSource2</code>. Of course you need to declare your second <code>dataSource</code> and other beans: simply copy the configuration you already have and change bean ids so they won't collide. Everything should be mirrored except <code>&lt;tx:annotation-driven/&gt;</code>:</p> <pre><code>&lt;bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt; &lt;!-- ... --&gt; &lt;/bean&gt; &lt;bean id="sessionFactory1" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource1"/&gt; &lt;!-- ... --&gt; &lt;/bean&gt; &lt;bean id="transactionManager1" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt; &lt;property name="sessionFactory" ref="sessionFactory1"/&gt; &lt;!-- ... --&gt; &lt;/bean&gt; &lt;bean id="dataSource2" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt; &lt;!-- ... --&gt; &lt;/bean&gt; &lt;bean id="sessionFactory2" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource2"/&gt; &lt;!-- ... --&gt; &lt;/bean&gt; &lt;bean id="transactionManager2" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt; &lt;property name="sessionFactory" ref="sessionFactory2"/&gt; &lt;!-- ... --&gt; &lt;/bean&gt; &lt;tx:annotation-driven transaction-manager="transactionManager1"/&gt; </code></pre> <p>And here comes the real problem: you now have two transaction managers bound to different session factories, which in turns are routed to different data sources. But <code>@Transactional</code> annotations will always use only one transaction manager - the one named <code>transactionManager</code> by default (note I explicitly pointed <code>transactionManager1</code>. This means that DAOs using second data source will participate in transactions started within first data source - this is obviously not what intended.</p> <p>There are some workaround to this, like explicitly defining transaction manager name in <code>@Transactional</code> annotation (never tried it) or using <code>TransactionTemplate</code>, but as you can see problem should be well thought.</p> <p>As for autowiring - if you autowire by name, name your fields the same as session factories or data sources ids and it should work - but is your smallest problem actually.</p>
 

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