Spring entityManagerFactory in jta and non-jta modes

This blog post is about using JPA with Spring in 2 contexts :

  • production with a JTA transaction manager
  • testing with transactions handled by jpa transaction manager.
    It has been inspired by Erich Soomsam blog postYou can achieve such configuration with a PersistenceUnitPostProcessor having a single persistence.xml file and 2 Spring context files (1 for each environment).Since you are likely to have at least 2 different Spring dataSource definitions : 1 for production that performs a JNDI lookup to find a bound datasource and 1 for development that uses a local and Spring declared datasource backed by a JDBC connection pool (C3p0 or DBCP), place the entityManager declaration in the same file as the datasource declaration. Let’s say that the default persistence.xml use the non-jta datasource:
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemalocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">
    <persistence-unit transaction-type="RESOURCE_LOCAL"
    name="seamphony">
    <properties> <!-- Scan for annotated classes and Hibernate mapping XML files -->
    <property value="class, hbm" name="hibernate.archive.autodetection" />
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
    </properties>
    </persistence-unit>
    </persistence>

    Here’s how you can use Spring to post process the persistence unit and configure it for production (here with MySQL datasource and JBoss Transaction Manager):
    <bean
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    id="entityManagerFactory">
    <property ref="dataSource" name="dataSource"></property>
    <property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property value="MYSQL" name="database" />
    <property value="true" name="showSql" />
    <property value="org.hibernate.dialect.MySQLDialect" name="databasePlatform" />
    </bean>
    </property>
    <property name="jpaPropertyMap" />
    </bean>
    <map>
    <entry value="org.hibernate.transaction.JBossTransactionManagerLookup"
    key="hibernate.transaction.manager_lookup_class">
    <entry value="true" key="hibernate.transaction.flush_before_completion" />
    <entry value="true" key="hibernate.transaction.auto_close_session" />
    <entry value="jta" key="hibernate.current_session_context_class" />
    <entry value="auto" key="hibernate.connection.release_mode" />
    </entry>
    </map>
    <property name="persistenceUnitPostProcessors">
    <list>
    <bean class="JtaPersistenceUnitPostProcessor">
    <property value="true" name="jtaMode"></property>
    <property ref="dataSource" name="jtaDataSource"></property>
    </bean>
    </list>
    </property><!-- Datasource Lookup -->
    <bean class="org.springframework.jndi.JndiObjectFactoryBean" id="dataSource">
    <property name="resourceRef">
    <value>false</value>
    </property>
    <property name="jndiName">
    <value>java:/MyDS</value>
    </property>
    </bean><!-- Transaction Manager -->
    <bean class="org.springframework.transaction.jta.JtaTransactionManager"
    id="transactionManager">
    <property value="java:/TransactionManager" name="transactionManagerName" />
    <property value="false" name="autodetectUserTransaction" />
    </bean>

Here’s the class that reads the jta mode property and configure the transaction type accordingly:

import javax.persistence.spi.PersistenceUnitTransactionType;
import javax.sql.DataSource;
import org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor;
public class JtaPersistenceUnitPostProcessor implements
PersistenceUnitPostProcessor {
private boolean jtaMode = false;
private DataSource jtaDataSource;
private PersistenceUnitTransactionType transacType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
public void postProcessPersistenceUnitInfo(
MutablePersistenceUnitInfo mutablePersistenceUnitInfo) {
if (jtaMode) {
transacType = PersistenceUnitTransactionType.JTA;
mutablePersistenceUnitInfo
.setJtaDataSource(this.getJtaDataSource());
}
mutablePersistenceUnitInfo.setTransactionType(transacType);
}
public boolean isJtaMode() {
return jtaMode;
}
public void setJtaMode(boolean jtaMode) {
this.jtaMode = jtaMode;
}
public DataSource getJtaDataSource() {
return jtaDataSource;
}
public void setJtaDataSource(DataSource jtaDataSource) {
this.jtaDataSource = jtaDataSource;
}
}

Spring really helps tuning your persistence unit for different environments. It could be achieved by a custom build task that could alter the persistence.xml file but since this example assumes that Spring is already used, it can be avoided.

Stat your commits

I recently discover the StatSCM maven plugin and it made our team day . It gives useful information about your Subversion activity (it supports other SCM systems) giving precise developers activity information or file statistics. It also generates some nice charts. I found it very useful to monitor what the team members commit: for instance we discovered that a trainee was the developer of the month (ranked by LOC commited) because he commited some very large csv test files.First to enable it, configure the dependency and declare the report in the reports section of your maven parent pom file:

<pom>
<build>
<plugins>
<plugin>
<groupid>net.sf</groupid>
<artifactid>stat-scm</artifactid>
<dependencies>
<dependency>
<groupid>net.sf</groupid>
<artifactid>stat-svn</artifactid>
<version>0.4.0-StatSCM</version>
</dependency>
<dependency>
<groupid>org.apache.maven.reporting</groupid>
<artifactid>maven-reporting-impl</artifactid>
<version>2.0.4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupid>net.sf</groupid>
<artifactid>stat-scm</artifactid>
<configuration>
<excludes>
<exclude>**/*.csv</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</reporting>
</pom>

And here some charts produced, like the number of LOC commited by each developer:Number of lines of codeThe day commit activity:Commits activityOr the ration between files addition/modification: