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.

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.

Wednesday, July 1, 2009

Add Any Gadget to Your Gmail (including ours)

After watching a presensation online at Google I/O , they demonstrated that in addition to being able to add gadgets to you google home page ( www.google.com/igoogle), you can also add gadgets to you gmail page.
Here is how you do it:
To add a gadget to the left nav of your Gmail account, follow these steps:

1. Go to the 'Labs' tab under Gmail Settings.
2. Enable the 'Add any gadget by URL' experiment and click 'Save Changes.'
3. Now you'll have a 'Gadgets' tab under Settings.
4. Enter the URL of an OpenSocial gadget spec and click 'Add'.

In EnduroTracker.com's case the url is: http://endurosocial.googlecode.com/svn/trunk/trunk/qaendurotrackeroauth.xml

Wednesday, June 17, 2009

On Newsstands now, Wired - July 2009 - Athletes - Track your Data

As software developer who works on a site that does exactly this,
I was very pleased to see the cover of Wired this month (July 2009) covering
Athletes who now Track their data using Gps devices, heart rate monitors, etc.
and also use different websites similar to mine (http://www.EnduroTracker.com )to plan, track, and analyze their data.

Thursday, June 11, 2009

Gadgets and Widgets Oh My!

In this blog post I thought I would talk about OpenSocial Gadgets since Google starting promoting Gadgets last week by way
of having celebrities share their iGoogle setup with selected Gadgets.
At a high level a container site like iGoogle (http://www.google.com/igoogle) let's users add gadgets to their customizable home page.
A gadget is a web application in itself, and can do pretty much anything a standard web page can do (except its real estate/size on page is restricted).
For example, you can add Espn's gadget to your iGoogle page, and it will display headlines from Espn. You can also add things like a weather gadget, and even a Google gmail gadget so you can read your email without leaving iGoogle. As an avid internet user I find that after a while, I can end up with 20 or more browser tabs or browser windows open when I'm browsing thru my favorite sites. However, if all my favorite sites had gadgets I could just have 1 tab open to iGoogle and I browse my favorites from all in one place. It is great for consolidating and streamlining how you use the internet.
Being a fan of making users internet experience better, I took the plunge and developed a OpenSocial Gadget for http://www.EnduroTracker.com.
To add the Gadget to your iGoogle page just click the google icon on the bottom of the site.
If you are developer and want to take the plunge I have a few suggestions.
There is some good documentation out there. The first documentation to read is the igoogle developer's guide, http://code.google.com/apis/igoogle/docs/igoogledevguide.html
After reading thru the majority of this documentation, it is a good idea to create a simple Hello World gadget using google's sandbox.
If you need to utilize security/authentication to pull data from your site, then you will need to implement OAuth. It is a good idea to first read the documentation and demos
at http://oauth.net .
A good code example of using OAuth from both a provider and consumer perspective, is http://lab.madgex.com/oauth-net/
If you are going down the OAuth path and are choosing to create a Gadget for iGoogle, your site will need to implemented an OAuth Provider.
I have implemented an OAuth provider using the Open Source MVC framework for .Net (C#) Castle MonoRail, if anyone using Castle MonoRail is interested in
viewing or using my OAuth provider code let me know by leaving a comment and I can share it via code.google.com.
In a nutshell, you will need a Container (iGoogle), a OAuth provider (your site), and your Gadget code (an xml file).
Once you have picked your Container (google has one as does yahoo, myspace, etc), written your OAuth provider code, and have a some Gadget code,
you should test all your code locally. This way you can have your own local sandbox to step thru your code as you work out the kinks.
How can I implement a Container like igoogle locally? Well, the short answer is that you can use Shindig which is an open source container.
It is available here: http://incubator.apache.org/shindig/
If you want to setup Shindig, see my March 16th blog entry below.
There are major benefits to using OpenSocial and OAuth. Both projects are standards driven meaning that they work with lots of different sites and vendors
to come up with best standards for all parties involved. This means that lots of sites are using OpenSocial and OAuth. The major players being Google, Yahoo,
Twitter, and MySpace. Once you implemented OpenSocial and OAuth for one of these sites, it will be very easy to integrate with the other sites. This means developers
don't have to write separate integration code for each site. Developers can use the DRY principle (don't repeat yourself).

Thursday, June 4, 2009

Nice Article on General Setup of Fedora 9

Here is a decent article on how to setup Fedora 9 in general using yum.
http://www.mjmwired.net/resources/mjm-fedora-f9.html#yum