Showing posts with label seam. Show all posts
Showing posts with label seam. Show all posts

Tuesday, March 15, 2011

Understanding seam's persistence context configuration

Actually this post is a note for myself regarding how seam could be configured in the web application. The facts are:
- The application is running on JBoss 5.1
- Seam is used to manage the lifecycle of EntityManager instances

Here's the configuration:
  1. Datasource configuration file placed in {server}/deploy
    JNDI name is Datasource
  2. Persistence unit configuration in persistence.xml
    <persistence-unit name="PersistenceUnit" transaction-type="JTA">
    <provider
    >org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>Datasource</jta-data-source>
    ......
    <properties
    >
    ........
    <property name="jboss.entity.manager.factory.jndi.name" value="java:/EntityManagerFactory"/>
    </properties>


  3. Declaration of persistence-unit-ref element in web.xml
    <persistence-unit-ref>
    <persistence-unit-ref-name>PersistenceUnit/pu</persistence-unit-ref-name>
    <persistence-unit-name>PersistenceUnit</persistence-unit-name>
    </persistence-unit-ref>

  4. Configuration in component.properties
    seamBootstrapsPu=false
    seamEmfRef=#{null}
    puJndiName=java:comp/env/PersistenceUnit/pu
  5. Configuration of persistence managed context in components.xml
    <persistence:managed-persistence-context name="entityManager" auto-create="true"
    entity-manager-factory="@seamEmfRef@"
    persistence-unit-jndi-name="@puJndiName@"/>


Here's the short explanation:
  1. A new datasource is deployed (step 1)
  2. The datasource is referred to in persistence.xml (step 2)
  3. Add reference to the new persistence unit name in web.xml (step 3)
  4. Add new property in component.properties which refers to the persistence-unit-ref-name declared in web.xml (step 4)
  5. The values configured in component.properties is used to configure the managed-persistence-context (step 5)

Note: The entity manager factory is bound to the global JNDI thru "jboss.entity.manager.factory.jndi.name" property configured during step 2, however it is not used... yet :)

Tuesday, September 14, 2010

Rolling back transaction (Seam managed transaction)

JSF+Seam is what I'm learning at the moment. And the case is I need to rollback the transaction due to business exception but I don't want to not catch the transaction (when a runtime exception is uncaught the transaction will be rollback automatically). The business exception here is a new exception class extending RuntimeException.

At first try, I add ApplicationException annotation with rollback = true in my BusinessException class. The controller class which invokes the business service class and under certain condition an exception of BusinessException type is thrown. In my scenario my transaction should be rolled back, the thing is... it was not rolled back.

This forced me to dive into seam's code (that's why I love open source) and find out how to handle the transaction. And I came into Transaction class, which I can use to do what I want.
Here's how to use it to mark the transaction as rollback:

Transaction.instance().rollback();

Actually all I wish is when the exception is thrown by the service class (already registered to seam) the transaction will be rolled back automatically, unfortunately for me it doesn't work :(