Showing posts with label Spring Multiple Database Java JTA Hibernate. Show all posts
Showing posts with label Spring Multiple Database Java JTA Hibernate. Show all posts

Wednesday, August 12, 2009

Using Spring with multiple databases Part 2

If you want to debug the Spring and Hibernate source code, using your SVN client of choice download the prerequisites (or if you use Maven you can update your pom.xml to reference the applicable jar files).
For Spring the version is 3.0.0.Build-SNAPSHOT.
Since, we are using the Trunk (the bleeding edge), it is probably best to be able to debug their code, so I suggest you download the source code. Additionally, you will be able to more easily contribute patches if you like.

Spring 3.0 Framework Source Control Repository (SVN): https://src.springframework.org/svn/spring-framework/trunk

Hibernate Trunk SVN: http://anonsvn.jboss.org/repos/hibernate/core/trunk

To download Atomikos, go to http://www.atomikos.com/Main/TransactionsEssentials

Atomikos is open source but they have you register prior to downloading the code.

When developing web applications in Java, you can end up with a lot of lines of xml code in your config files. To alleviate the number of lines of xml configuration code, Spring introduced Annotations which allow you to add Annotations or configuration settings within your classes. The key Annotation that we will be using is @Transactional which tells the JVM to create a transaction for this class or method. Spring also has Annotations like @Controller that tells the JVM that this class is a Controller. This allows developers to cut down on the amount of xml configurations that they need to specify. Unfortunately, we still to have some xml configuration, so let's setup our datasources xml in our applicationcontext.xml file.

Here is the top section of our xml (applicationcontext.xml) file:


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">


In order to the Atomikos Transaction Manager, our datasources need to be configured
using Atomikos datasource classes. Using other datasources class types won't work.
Here is an example of how to configure the datasources:

<!-- using Atomikos DataSources -->
<bean id="dataSource" class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean">
<property name="uniqueResourceName"><value>NONXADBMS</value></property>
<property name="user"><value>admin</value></property>
<property name="password"><value>password</value></property>
<property name="url"><value>jdbc:postgresql://localhost/usersdb</value></property>
<property name="driverClassName"><value>org.postgresql.Driver</value></property>
<property name="poolSize"><value>1</value></property>
<property name="borrowConnectionTimeout"><value>60</value></property>
</bean>

<bean id="dataSource2" class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean">
<property name="uniqueResourceName"><value>NONXADBMS2</value></property>
<property name="user"><value>admin</value></property>
<property name="password"><value>password</value></property>
<property name="url"><value>jdbc:postgresql://localhost/accountingdb</value></property>
<property name="driverClassName"><value>org.postgresql.Driver</value></property>
<property name="poolSize"><value>1</value></property>
<property name="borrowConnectionTimeout"><value>60</value></property>
</bean>


In part 3, we will setup our SessionFactories.

Tuesday, August 11, 2009

Using Spring with multiple databases Part 1

This series of articles is about how to use the Spring framework within your application to reference multiple databases or in Java parlance datasources.
In a previous project, I used NHibernate and ActiveRecord and I was able to relatively painlessly use multiple databases. In Spring, to use multiple databases it took longer since my googling came up with some examples , but not complete enough that when tested gave me what I needed. In the Spring framework world and in the Java world in general there all several database persistences technologies. There is plain vanilla Hibernate, there is JPA via Hibernate, there is JPA via TopLink, there is Spring's HibernateTemplating, and plenty more.
To setup Spring with multiple databases, we are going to do the following:
Prerequisite:
Download and setup SpringFramework 3.0 from the Trunk.
Download and setup Hibernate 3.5 from the Trunk.
Download and setup Atomikos Transaction Manager. (optional download sources)
1) Setup our datasources in our applicationcontext.xml
2) Setup SessionFactory configurations in our applicationcontext.xml
3) Setup JTATransactionManager configurations in our applicationcontext.xml
4) Create a custom JTATransactionFactory class to deal with Hibernate bug: HHH-3110
5) Configure Atomikos (turn off logging on Atomikos)
6) Configure Dao and Controllers with @Transactional annotation
Editors Note: Need @Transactional annotation in Controller , having @Transactional in Dao did not always create a Transaction.
7) Create Unit Tests
8) Take a break!

In part 2, we will setup our datasources. In part 3, we will setup our SessionFactories.