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.

No comments:

Post a Comment