2011-05-29

JTA transactions with Hibernate, JBoss 6 and Spring 3

In this post I will show you how to use JTA transactions with JBoss 6 server, Spring 3 and Hibernate 3.

In JEE, JTA allows transactions to be distributed across multiple resources. You don't need a JEE server though to run JTA transactions, Tomcat will do (possibly with a bit more hassle).
I decided to use JBoss 6 because it is a popular JEE server - but I would assume you can successfully run the code on any server as long as the data source is set correctly (plus with minor tweaks of server dependent stuff).

(I also wrote a post on non-jta transactions with Spring 3, JBoss 6 and Hibernate 3.)

applicationContext.xml


This is our Spring config file.

<bean id="entityManagerFactory"
       class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
       <property name="persistenceUnitName" value="Dogs" />
   </bean>

   <bean id="dogsDao" class="me.m1key.springtx.dao.DogsDaoImpl" />

   <bean id="dogsBean" class="me.m1key.springtx.beans.DogsBean"
       scope="singleton">
       <property name="dogsDao" ref="dogsDao" />
   </bean>

   <tx:annotation-driven transaction-manager="myTransactionManager" />
   <tx:jta-transaction-manager />

   <bean id="myTransactionManager"
       class="org.springframework.transaction.jta.JtaTransactionManager" />

   <bean
       class="
       org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor " />
Look closely, notice the jta-transaction-manager annotation and JtaTransactionManager.

persistence.xml


This is our persistence.xml. It's a bit different from the non-jta version.

…
    <persistence-unit name="Dogs" transaction-type="JTA">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/DogsDS</jta-data-source>
       <class>me.m1key.springtx.entities.Dog</class>

       <properties>
           <property name="hibernate.transaction.manager_lookup_class"
               value="org.hibernate.transaction.JBossTransactionManagerLookup" />
           <property name="hibernate.show_sql" value="true" />
           <property name="hibernate.format_sql" value="true" />
           <property name="hibernate.hbm2ddl.auto" value="create" />
       </properties>
   </persistence-unit>
…
Notice the transaction-type attribute. It's JTA as opposed to RESOURCE_LOCAL in the case of non-JTA. You must also specify transaction manager lookup class which is server dependent.

DAO


@PersistenceContext
  private EntityManager em;

  @SuppressWarnings("unchecked")
  @Transactional(readOnly = true)
  public List<Dog> retrieveAllDogs() {
      return em.createQuery("from Dog").getResultList();
  }

That's it! To run this example you need a database server running. I'm using HSQLDB. You can download source code and there is a batch file there that runs this HSQLDB server. The application is to be deployed on JBoss 6.

You call the transactional operation by accessing http://localhost:8080/springtx-jta-0.0.1-SNAPSHOT/myurl.

pom.xml


For reference, the relevant parts of the POM file.

<dependencies>
       <dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-core</artifactId>
           <version>3.6.0.Final</version>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-commons-annotations</artifactId>
           <version>3.2.0.Final</version>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.hibernate.javax.persistence</groupId>
           <artifactId>hibernate-jpa-2.0-api</artifactId>
           <version>1.0.0.Final</version>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-entitymanager</artifactId>
           <version>3.6.0.Final</version>
           <type>jar</type>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>hsqldb</groupId>
           <artifactId>hsqldb</artifactId>
           <version>1.8.0.7</version>
           <type>jar</type>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-core</artifactId>
           <version>3.0.3.RELEASE</version>
           <type>jar</type>
           <scope>compile</scope>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-aop</artifactId>
           <version>3.0.3.RELEASE</version>
           <type>jar</type>
           <scope>compile</scope>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-tx</artifactId>
           <version>3.0.3.RELEASE</version>
           <type>jar</type>
           <scope>compile</scope>
       </dependency>
       <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-api</artifactId>
           <version>1.5.8</version>
       </dependency>
       <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-simple</artifactId>
           <version>1.5.8</version>
       </dependency>
       <dependency>
           <groupId>commons-collections</groupId>
           <artifactId>commons-collections</artifactId>
           <version>3.1</version>
           <type>jar</type>
           <scope>compile</scope>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-test</artifactId>
           <version>3.0.3.RELEASE</version>
           <type>jar</type>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.8.1</version>
           <type>jar</type>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>3.0.3.RELEASE</version>
           <type>jar</type>
           <scope>compile</scope>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-orm</artifactId>
           <version>3.0.3.RELEASE</version>
           <type>jar</type>
           <scope>compile</scope>
       </dependency>
       <dependency>
           <groupId>org.jboss.spec.javax.servlet</groupId>
           <artifactId>jboss-servlet-api_3.0_spec</artifactId>
           <version>1.0.0.Final</version>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>cglib</groupId>
           <artifactId>cglib</artifactId>
           <version>2.2</version>
       </dependency>
   </dependencies>
   <repositories>
       <repository>
           <id>r.j.o-groups-public</id>
           <url>https://repository.jboss.org/nexus/content/groups/public/</url>
       </repository>
   </repositories>

2011-05-25

Non JTA transactions with Hibernate, JBoss 6 and Spring 3

Hi there, I'm going to show you how to set up your JEE project to use non-JTA transactions with JBoss AS 6, Spring 3, Hibernate and JPA annotations.
Please note - integration tests don't require a database server running - they instantiate their own in-memory database server.

(Interested in JTA with Hibernate 3, Spring 3 and JBoss 6 instead?)

applicationContext.xml


This is our Spring config file.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

   <bean id="entityManagerFactory"
       class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
       <property name="persistenceUnitName" value="Dogs" />
   </bean>

   <bean id="dogsDao" class="me.m1key.springtx.dao.DogsDaoImpl" />

   <bean id="dogsBean" class="me.m1key.springtx.beans.DogsBean"
       scope="singleton">
       <property name="dogsDao" ref="dogsDao" />
   </bean>

   <tx:annotation-driven transaction-manager="myTransactionManager" />

   <bean id="myTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
       <property name="entityManagerFactory" ref="entityManagerFactory" />
   </bean>

   <bean
       class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>
What's worth noting is the transaction manager. Because we are not using JTA, we must explicitly provide the transaction manager for the application to use.

persistence.xml


Next, let's look at our persistence.xml file.

…
<persistence-unit name="Dogs" transaction-type="RESOURCE_LOCAL">       <non-jta-data-source>java:/DogsDS</non-jta-data-source>
      <class>me.m1key.springtx.entities.Dog</class>

      <properties>
          <property name="hibernate.show_sql" value="true" />
          <property name="hibernate.format_sql" value="true" />
          <property name="hibernate.hbm2ddl.auto" value="create" />
      </properties>
  </persistence-unit>
...
What we have here is a persistence unit called Dogs. The transaction type is RESOURCE_LOCAL - that tells the container not to use JTA. Below we specify JNDI data source name, entity classes and some properties. Note I don't have to specify typical data source properties (user name, password, url) - that's because they don't belong here. We define data source properties in JBoss.

dogs-ds.xml


This is a standard JBoss data source file. It must be called X-ds.xml.

<?xml version="1.0" encoding="UTF-8"?>

<datasources>
   <local-tx-datasource>

       <jndi-name>DogsDS</jndi-name>
       <connection-url>jdbc:hsqldb:hsql://localhost/dogsdb</connection-url>
       <driver-class>org.hsqldb.jdbcDriver</driver-class>
       <user-name>sa</user-name>
       <password></password>
       <min-pool-size>5</min-pool-size>
       <max-pool-size>20</max-pool-size>
       <idle-timeout-minutes>0</idle-timeout-minutes>
       <track-statements />
       <prepared-statement-cache-size>32</prepared-statement-cache-size>
       <metadata>
           <type-mapping>Hypersonic SQL</type-mapping>
       </metadata>
   </local-tx-datasource>
</datasources>
Nice and simple.

DAO


Last but not least, DAO that uses this setup.

   @PersistenceContext
   private EntityManager em;

   @SuppressWarnings("unchecked")
   @Transactional(readOnly = true)
   public List<Dog> retrieveAllDogs() {
       return em.createQuery("from Dog").getResultList();
   }

That's it! To run this example you need a database server running. I'm using HSQLDB. You can download source code and there is a batch file there that runs this HSQLDB server. The application is to be deployed on JBoss 6.

You call the transactional operation by accessing http://localhost:8080/springtx-nonjta-0.0.1-SNAPSHOT/myurl.

2011-05-20

git - compare branches

How to see the difference between two branches in git?

To display different lines:
git diff master..branch

To display different files:
git diff --name-status master..branch

You can make your console use colours to emphasise differences too:
git diff --color master..my251

You can swap branches to view it from the other perspective (depending on how you want to merge):
git diff branch..master

2011-05-19

APPARENT DEADLOCK with c3p0

When using Hibernate, JBoss 6, MySQL/HSQLDB, c3p0 for connection pooling, I got the following error (not a warning, as the log level seems to suggest) while running transactional JPA code.

WARN  [com.mchange.v2.async.ThreadPoolAsynchronousRunner]
(Timer-1) com.mchange.v2.async.ThreadPoolAsynchronousRunner
$DeadlockDetector@26d6221b -- APPARENT DEADLOCK!!!
(line breaks added for readability)

I'm sure there might be more than one reason for this. However, in my case what caused it was duplicate c3p0 declarations - I had them in two places:
  • persistence.xml file
  • dogs-ds.xml file (JBoss data source file)

The solution was to remove these lines from persistence.xml (which makes sense since the data source declaration is JBoss' job):

<property name="hibernate.c3p0.max_size" value="100" />
...

No more deadlock!

2011-05-13

git - format patch for single commit

With git, how to create a patch for a single commit?

git format-patch commit_id^..commit_id

2011-05-05

AspectJ: this() vs. target()

this() and target() are AspectJ poincuts for collecting context. What is the difference between them?

this()


That is the Java this object at the matching point.

target()


That is the Java object on which the method is called (or the object whose field is accessed).


… So, what’s the difference in practice? It turns out, it depends on the execution model you use - whether it’s execution or call.

Execution example


execution( void Dog.bark() ) && this( thisObject ) && target( targetObject )
thisObject will be a dog instance. targetObject will be the same dog instance. (If you’re wondering why, it depends on how AspectJ works under the hood - for execution it builds in your advice into the advised method.)

Call example


call( void Dog.bark() ) && this( thisObject ) && target( targetObject )
thisObject will be the instance that called dog.bark(). It could be any class that made this call. targetObject will, still, be a dog instance. (If you’re wondering why, it depends on how AspectJ works under the hood - for call it builds in your advice at the method call, not inside it.)

2011-05-04

AOP: AspectJ field access to inject dependencies

Since I’m reading a book on AspectJ, I figured out it would be possible to use AOP for dependency injection. I was sure the code would be concise and elegant, but I was wrong. Let’s start from the beginning.

This is what I wanted to have:

@Log
private static Logger log;

As you can see, I have a private static field, no getters or setters for it, it’s annotated with my custom @Log annotation.

Now, I wanted to use AspectJ to
  • seamlessly initialize this field
  • do it only once for every class where it’s used
  • and I wanted the initialized logger to be a logger for this particular class

Solution


Sadly, that’s the best I was able to come up with.

package me.m1key.test;

import java.lang.reflect.Field;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public aspect LogInjectAspect {

    pointcut loggerAccessed(Object a): get(@Log Logger me.m1key.test.*.*) && this(a);

    before(Object objectWithLog): loggerAccessed(objectWithLog) {
        for (Field field : objectWithLog.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            if (field.getName().equals(thisJoinPoint.getSignature().getName())) {
                try {
                    if (field.get(null) == null) {
                        System.out.println("OMG I'm setting the value.");
                            field.set(objectWithLog, LoggerFactory.getLogger(objectWithLog.getClass()));
                    }
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (SecurityException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

I achieved what I wanted to achieve, but I had to use reflection. Also, this code fires every time log is accessed, although it is initialized only once (there’s a check). The code is pretty ugly and convoluted.
Shame, but that’s the best I could do.

Conclusion: use CDI for this purpose.

2011-05-01

duplicate key: interface javax.validation.constraints.Size

I got this error from JBoss while playing with RichFaces 4:

duplicate key: interface javax.validation.constraints.Size

JBoss and RichFaces 4 are not to blame - I am. I realized I switched RichFaces 4 versions (from 4.0.0.Final to 4.0.1-SNAPSHOT) and deployed both with my WAR file. So if you get this error, perhaps that’s what you should look into first.

RichFaces 4: More components

Hi, this is my third post on RichFaces 4. I’d like you to download the source code for this article and run it yourself (I run it on JBoss 6.0.0.Final). It does nothing very fancy, but it demonstrates a few RichFaces 4 components, and JSF 2.0 (that is, CDI, new annotations, Internationalization and more).

Below, highlights of the sample app.

rich:accordion


rich:accordion is a very simple component that allows you to have collapsible panels (and only one is open at a time).

Accordion

       <rich:accordion switchType="client">
           <rich:accordionItem header="#{msg['index.accordion.tab1.heading']}">
               <h:outputText value="#{msg['index.accordion.tab1.text']}" />
           </rich:accordionItem>
           <rich:accordionItem header="#{msg['index.accordion.tab2.heading']}">
               <h:outputText value="#{msg['index.accordion.tab2.text']}" />
           </rich:accordionItem>
       </rich:accordion>

Modal pop-up panel


Here’s a modal pop-up panel. You need to close it before all other page elements become accessible.

Modal Panel

<rich:popupPanel id="popup" modal="true" resizeable="false"
   onmaskclick="#{rich:component('popup')}.hide()">
   <f:facet name="header">
       <h:outputText value="#{msg['index.info.header']}" />
   </f:facet>
   <f:facet name="controls">
       <h:outputLink value="#"
           onclick="#{rich:component('popup')}.hide(); return false;">
       X
       </h:outputLink>
   </f:facet>
   <p>
       <h:outputText value="#{msg['index.info.text']}" />
   </p>
   <p>
       <h:outputLink value="#"
           onclick="#{rich:component('popup')}.hide(); return false;">
           <h:outputText value="#{msg['index.info.close']}" />
       </h:outputLink>
   </p>
</rich:popupPanel>

rich:dataTable


And now a dataTable. This is a fairly sophisticated component and you can see more if you browse RichFaces 4 showcase.

Data table

<h:form id="dogForm">
           <rich:messages />
           <rich:dataTable value="#{dogsBean.allDogs}" var="dog" id="table"
               rows="30">
               <f:facet name="noData">
                   <h:outputText value="#{msg['index.table.noData']}" />
               </f:facet>
               <rich:column filterValue="#{dogsFilteringBean.nameFilter}"
                   filterExpression="#{fn:containsIgnoreCase(dog.name,dogsFilteringBean.nameFilter)}">
                   <f:facet name="header">
                       <h:panelGroup>
                           <h:outputText value="#{msg['index.table.column.name']}" />
                           <h:inputText value="#{dogsFilteringBean.nameFilter}"
                               onkeypress="if (event.keyCode == 13) {return false;} else {return true;}">
                               <a4j:ajax event="blur" render="table" execute="@this" />
                           </h:inputText>
                       </h:panelGroup>
                   </f:facet>
                   <h:outputText value="#{dog.name}" />
               </rich:column>
           </rich:dataTable>
       </h:form>

As you can see, it allows you to filter data (and it’s AJAX based).

One thing you must take into account is that if focus is in the filter field and the Enter key is pressed - it will submit the form. I don’t think that’s the desired behavior, so I put a hack in there (onkeypress). I reported this as a bug and we’ll see what they answer.

Internationalization - I18N


This is not RichFaces 4 dependent, but since it’s used in the code above, I will demonstrate how it works.

WEB-INF/faces-config.xml

I have the following declaration in my faces-config.xml.

<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
   version="2.0">
   <application>
       <locale-config>
           <default-locale>en</default-locale>
       </locale-config>
       <resource-bundle>
           <base-name>messages.messages</base-name>
           <var>msg</var>
       </resource-bundle>
   </application>
</faces-config>

messages/messages.properties

And this is a properties file that the previous declaration points to. It holds all the internationalized messages:

registration.title = Register
registration.h1 = Register
registration.email = Email:
registration.name = Name:
...

Usage

Then I can use it on my pages.

<h:outputText value="#{msg['registration.h1']}" />

Summary


In this article I demonstrated some RichFaces 4 components. The application itself has more goodies in it that you may want to check out. I wrote two more posts on RichFaces 4:

Don’t forget to download the source code for this article.