Wednesday, August 12, 2009

Using Spring with multiple databases Part 3

In this part, we setup our SessionFactories in our applicationcontext.xml file.
In our SessionFactories we specify our datasource, our entity classes, and also our hibernate properties.
Since we are using multiple databases, in this case 2 databases, we need to create two SessionFactory configurations.
Here is our first SessionFactory config that we place in applicationcontext.xml:

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.endurotracker.gwt.model.Users</value>

</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.connection.isolation">3</prop>
<prop key="hibernate.current_session_context_class">jta</prop>
<prop key="hibernate.transaction.factory_class">com.endurotracker.gwt.transaction.AtomikosJTATransactionFactory</prop>
<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
</props>
</property>
</bean>




Here is our second SessionFactory config that we place in applicationcontext.xml:

<!--Hibernate SessionFactory2-->
<bean id="sessionFactory2"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource2" />
<property name="annotatedClasses">
<list>
<value>com.endurotracker.gwt.model.Activitytype</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.connection.isolation">3</prop>
<prop key="hibernate.current_session_context_class">jta</prop>
<prop key="hibernate.transaction.factory_class">com.endurotracker.gwt.transaction.AtomikosJTATransactionFactory</prop>
<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
</props>
</property>
</bean>


You will notice that we have 2 hibernate properties related to transactions:
hibernate.transaction.factory_class and hibernate.transaction.manager_lookup_class.
Due to a Hibernate bug ( bug HHH-3110), we had to create the following class which we reference as com.endurotracker.gwt.transaction.AtomikosJTATransactionFactory, so you can take this code and create your own class as well and update this property to your class name:

public class AtomikosJTATransactionFactory extends JTATransactionFactory{
UserTransaction userTransaction;

@Override
protected UserTransaction getUserTransaction() {
if (this.userTransaction == null)
{
this.userTransaction = new UserTransactionImp();
}

return this.userTransaction;

}

}


In Part 4, we will configure our Atomikos Transaction Manager via Atomikos' Transaction beans, and talk about how to use @Transactional Annotations in our Data Access layer and Controller layer. In Part 5, we will setup some unit tests using JUnit 4 and Spring's unit testing Annotations.

No comments:

Post a Comment