Updated Answer for new version Hibernate
Since hibernate 5 version, you must define transaction manger for every data source but if you are using general @Transaction annotation in your source code this will make confusion for transaction manager which one to pick, hibernate by default take the first define transaction manager in your xml file and use for all @Transaction annotation on any code. To avoid this problem you need to define transaction manager manually for every @Transaction on you code. Example:
<!-- Step 1: Define First Database DataSource / connection pool -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/spring_security_custom_user_demo?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC"/>
<property name="user" value="{user}"/>
<property name="password" value="{password}"/>
<!-- these are connection pool properties for C3P0 -->
<property name="minPoolSize" value="4"/>
<property name="maxPoolSize" value="20"/>
<property name="maxIdleTime" value="30000"/>
<property name="initialPoolSize" value="5"/>
</bean>
<!-- Step 2: Setup Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="packagesToScan" value="com.springdemo.entity"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Step 3: Setup Hibernate transaction manager -->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- end -->
<!-- Step 1: Define Second Database DataSource / connection pool -->
<bean id="myDataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/web_customer_tracker?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC"/>
<property name="user" value="{user}"/>
<property name="password" value="{password}"/>
<!-- these are connection pool properties for C3P0 -->
<property name="minPoolSize" value="4"/>
<property name="maxPoolSize" value="20"/>
<property name="maxIdleTime" value="30000"/>
<property name="initialPoolSize" value="5"/>
</bean>
<!-- Step 2: Setup Hibernate session factory -->
<bean id="sessionFactory2"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource2"/>
<property name="packagesToScan" value="com.springdemo.entity"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Step 3: Setup Hibernate transaction manager -->
<bean id="myTransactionManager2"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory2"/>
</bean>
<!-- Step 4: Enable configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="myTransactionManager"/>
<tx:annotation-driven transaction-manager="myTransactionManager2"/>
Here is @Transaction annotation example.
@Transactional("myTransactionManager")
public User findByUsername(String username) {
return userDao.findByUsername(username);
}
And if you need to refer to another transaction manager that uses another dataSource.
@Transactional("myTransactionManager2")
public User findByUsername(String username) {
return userDao.findByUsername(username);
}