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"
    xsi:schemalocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    <persistence-unit transaction-type="RESOURCE_LOCAL"
    <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" />

    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):
    <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" />
    <property name="jpaPropertyMap" />
    <entry value="org.hibernate.transaction.JBossTransactionManagerLookup"
    <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" />
    <property name="persistenceUnitPostProcessors">
    <bean class="JtaPersistenceUnitPostProcessor">
    <property value="true" name="jtaMode"></property>
    <property ref="dataSource" name="jtaDataSource"></property>
    </property><!-- Datasource Lookup -->
    <bean class="org.springframework.jndi.JndiObjectFactoryBean" id="dataSource">
    <property name="resourceRef">
    <property name="jndiName">
    </bean><!-- Transaction Manager -->
    <bean class="org.springframework.transaction.jta.JtaTransactionManager"
    <property value="java:/TransactionManager" name="transactionManagerName" />
    <property value="false" name="autodetectUserTransaction" />

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;
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:


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: