Dewavrin Luc ’s web site


Spring entityManagerFactory in jta and non-jta modes

Posted in tech, General, Java by luc.20.bengali on the June 23rd, 2008

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 post
You 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 name="seamphony" transaction-type="RESOURCE_LOCAL">
      <properties>
          <!-- Scan for annotated classes and Hibernate mapping XML files -->
          <property name="hibernate.archive.autodetection" value="class, hbm"/>
          <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 id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
   <property name="dataSource" ref="dataSource"></property>
   <property name="jpaVendorAdapter">
	<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL"></property>
            <property name="showSql" value="true"></property>
            <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"></property>
	</bean>
   </property>
   <property name="jpaPropertyMap">
      <map>
	<entry key="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
	<entry key="hibernate.transaction.flush_before_completion" value="true"/>
	<entry key="hibernate.transaction.auto_close_session" value="true"/>
	<entry key="hibernate.current_session_context_class" value="jta"/>
	<entry key="hibernate.connection.release_mode" value="auto"/>
      </map>
   </property>
   <property name="persistenceUnitPostProcessors">
      <list>
         <bean class="JtaPersistenceUnitPostProcessor">
            <property name="jtaMode" value="true"></property>
            <property name="jtaDataSource" ref="dataSource"></property>
         </bean>
      </list>
   </property>
</bean>
<!--
 Datasource Lookup
-->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
   <property name="resourceRef">
       <value>false</value>
    </property>
   <property name="jndiName">
      <value>java:/MyDS</value>
    </property>
</bean>
 
<!--
 Transaction Manager
-->
 
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
   <property name="transactionManagerName" value="java:/TransactionManager"></property>
   <property name="autodetectUserTransaction" value="false"></property>
</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

Posted in tech, Java by luc.20.bengali on the June 2nd, 2008

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 code

The day commit activity:


Commits activity

Or the ration between files addition/modification:


HP about to buy EDS

Posted in tech, General by luc.20.bengali on the May 12th, 2008

Concentration in IT goes on ( is it an effect of a “SAAS strategy” ?):

http://news.yahoo.com/s/nm/20080512/bs_nm/eds_hewlettpackard_dc_1

Opensource Java BPM compared

Posted in tech, Java by luc.20.bengali on the January 17th, 2008

(Seen on jBPM mailing-list): a quite interesting study between the non-exhaustive Java BPM engines (jBPM, enhydra shark, openwfe) has been published. Follow this link.

IT consolidation

Posted in tech by luc.20.bengali on the January 16th, 2008

Todays news:

Oracle buys BEA.

Mysql AB acquired by Sun.

Well what’s next? Redhat acquired by Sun (or even Microsoft), Adobe acquired by Oracle, etc ?

Industralization of the IT sector seems to get some steam, surely a sign of maturity. I am not sure if it will have an impact on innovation or this trend is the result of a loss of innovation. Big actors want to provide a complete enterprise stack from hardware to software package solutions. Will we only have to deal with SAP, Oracle, IBM, Sun and Microsoft tomorrow ?

Google strategy seems to differentiate and focus on online services.

A picture is worth a thousand words (my 2008 no interest techs list)

Posted in tech by luc.20.bengali on the January 3rd, 2008

This comic sums up my opinion on Scala pretty well (very funny):

http://stuffthathappens.com/blog/2008/01/02/scala-will-do/

I jumped on Ruby and Rails bandwagon spending some man-days (;-)) on it but at least Rails has been a reference in term of web productivity and introduced or leveraged nice concepts (naming conventions over configuration, DSL, REST, TDD etc.) . Recently, many Java web frameworks compared themselves to Rails and how they were much better (Seam,Grails & Co). Rails influenced the IT community (Java, .Net and Python included).

=> All in all, i have excuses to have followed the crowd… But not this time, Scala (pretty unreadable), JavaFx (not until i see the consumer jre in action and applets as fast-loaded as flash apps without dumb dialog boxes) are definitely on my 2008 technos of no-interest !

I’ll hopefully focus this year on the new Cobol ;-) core (or even .Net/C#) .

Next Page »