Project

General

Profile

Actions

Transaction management in Z2

This page provides some hints and pointers on how to implement transaction management for modular applications on Z2. There are a few utilities provided with Z2 that may help you implement transaction handling for the simple cases, and the site provides examples that show how to address the more complicated cases.

In short: You can use mostly any way of transaction management you can use in Java elsewhere.

A hint for Spring users: If your focus is on using Spring's built-in transaction management and data source configuration features, skip to Using Spring data sources and the Spring transaction manager.

The following aspects will be discussed:

The built-in, non-XA JTA implementation of Z2 (com.zfabrik.jta)

The z2-base.base repository contains the JTA 1.1 API and an implementation of it in the module com.zfabrik.jta.

Note: This is not an XA-capable transaction manager. This implementation provides no atomic transaction control for more than one transactional resource!

But, this implementation is still most useful, if you do not need distributed transactions (which is preferable in most cases) and still want to benefit from the JTA API as an integration point.

Technically the implementation relies on Z2's Unit of Work abstraction (see Unit of Work and transaction management). As long as all interactions with an application are issued from a Web application this is completely transparent. When invoking code from other entry points, e.g. from an embedded scheduling service for example, this has an impact that will be discussed below.

The TransactionManager interface, as well as the UserTransaction interface are available from the component com.zfabrik.jta/userTransaction and can be looked up via the Z2 core API

IComponentsLookup.INSTANCE.lookup("com.zfabrik.jta/userTransaction",TransactionManager.class)

or via JNDI as

new InitialContext().lookup("components:com.zfabrik.jta/userTransaction?type=javax.transaction.TransactionManager")

and similarly for the UserTransaction interface.

This simple approach to JTA has been used in the samples Sample-hibernate-basic and Sample-spring-hibernate.

Controlling transactions in code

Now that you have a JTA implementation you need to indicate where transactions start and end. The most basic (and most understandable) approach to that is to use the UserTransaction interface. Typically in a flow like this:

UserTransaction ut = (UserTransaction) new InitialContext().lookup( JNDI_NAME );
ut.begin();
try {
  // do some work
} catch (Exception e) {
  logger.log(Level.WARNING,"Error at transaction boundary. Setting transaction to rollbackonly",e);
  ut.setRollbackOnly();
  throw e;
} finally {
  if (ut.getStatus()==Status.STATUS_ACTIVE) {
    ut.commit();
  } else {
    ut.rollback();
  }
}

The constant JNDI_NAME is intentionally left undefined here. If you use com.zfabrik.jta you may use the name specified above. If the code above is part of a Servlet Filter that is supposed to begin and end transactions on the boundaries of a Web request, you can use the standard name "java:comp/UserTransaction" given you integrated the transaction management with Jetty (see below).

Now that we looked at it, typically you do not need to implement this yourself. Here are some alternatives:

  • Use TransactionUtil from
    com.zfabrik.jta that implements just the flow above.
  • Use any of the many "Transaction Filter" implementations out there.
If you are using Spring:

If you use Spring transaction management (over the JTA API), then you should also use Spring mechanisms for transaction demarcation. There is still great value in using JTA beneath, but Spring adds some of its own mechanisms, that are deeply tight in with Spring JPA for example, that will not work correctly using a pure JTA based demarcation.

Using Z2 DataSource components

A JDBC data source wraps JDBC connections, i.e. actual database connections, for re-use along the control flow.

When accessing the same database multiple times within one transaction, you want to make sure you re-use the same (underlying) JDBC connection. That is, you may (typically) not have more than one (underlying) JDBC connection to the very same database that share the same (non-distributed) transaction. Hence the typical implementation associates a JDBC connection to a database with the transaction that is associated with the current thread of execution. Obviously there is a strong tie between the management of database connections and the transaction management system. JDBC data sources provide the accessor interface to database connections. You may hold of to a data source and ask it for a connection when needed, but you should not hold on to the actual connection object between invocations of your service interface.

Z2 offers a built-in DataSource component type. This is useful, if you use com.zfabrik.jta. If you use another transaction management system, you should most likely use another data source implementation. See for example Sample-jta-plain.

When using the built-in data source, you can look up the DataSource interface via a component lookup, e.g. via JNDI like this:

(DataSource) new InitialContext().lookup(COMPONENT_NAME?type=javax.sql.DataSource)

where COMPONENT_NAME has to be substituted by the real name of the data source component.

The built-in data sources have been used for example in Sample-hibernate-basic and Sample-spring-hibernate.

An example definition can be found here: DB.

The component type javax.sql.DataSource is documented here: DataSources.

Using Hibernate and JPA with Z2 data sources

Now you have a JTA implementation in place as well as data sources defined, assuming you want to use the Java Persistence API you buy into yet another layer that needs to be integrated with the transaction management system.

In JPA, the equivalent of a data base connection is the EntityManager interface. And as with connections and data sources, you will want to reuse the entity manager instance across invocations within one transaction. An entity manager is created from an EntityManagerFactory given a definition of a Persistence Unit. We cannot go into all the details here.

In Java EE environments, you may delegate the creation of entity managers to the container and have it injected into your code using a corresponding annotation. The same comfort (and beyond that) can be achieved in Z2 even across modules using the Spring framework (see for example Sample-spring-hibernate and Sample-jta-spring).

The underlying implementation however is always about associating an entity manager with the ongoing transaction. If you do not want to use Spring or want to fully understand the principle mechanics, you may use the EntityManagerUtil of the module org.hibernate in *z2-base.base". This is how it is done in Sample-hibernate-basic.

To connect a persistence unit (and hence the entity manager factory) with the transaction management and the data source to use, you add corresponding information to the file META-INF/persistence.xml of your domain module. For example, the one in Sample-hibernate-basic looks like this:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
        <persistence-unit name="thingies" transaction-type="JTA">
                <provider>org.hibernate.ejb.HibernatePersistence</provider>
                <jta-data-source>components:com.zfabrik.samples.hibernate-basic.domain/DB
                </jta-data-source>
                <class>com.zfabrik.samples.hibernate_basic.thingies.Thingy</class>
                <exclude-unlisted-classes>true</exclude-unlisted-classes>
                <properties>
                        <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
                        <property name="hibernate.hbm2ddl.auto" value="update" />
                        <property name="hibernate.jdbc.batch_size" value="10" />
                        <property name="hibernate.cache.use_second_level_cache" value="false" />
                        <property name="hibernate.transaction.jta.platform" value="com.zfabrik.hibernate.Z2JtaPlatform"/>
                        <!-- previously to version 2.3 (or Hibernate version 4.3.x) you would use:
                        <property name="hibernate.transaction.manager_lookup_class" value="com.zfabrik.hibernate.TransactionManagerLookup" />
                        -->
                </properties>
        </persistence-unit>
</persistence>

The noteworthy pieces are

  1. The <jta-data-source/> definition that uses a DataSource JNDI name as discussed above.
  2. The <property name="hibernate.transaction.jta.platform"/> declaration

The latter is required for Hibernate to find the transaction manager instance. Unfortunately Hibernate does not support a JNDI name for that. That's why the module org.hibernate in the Hibernate Add-on provides an implementation you can use (it's really simple. See Z2JtaPlatform).

When not using com.zfabrik.jta, the value to specify differ of course. See also Sample-jta-plain.

Using a full-blown transaction manager

The sample Sample-jta-plain and Sample-jta-spring illustrate how to integrate the Atomikos transaction manager in a hand-wired application or in a Spring configured way resp. While it does help to understand the contents of this page, further details are to be found in the explanations of the samples.

Integration of non-Spring Transaction Managers with Spring

Integration with Spring is yet another subject on its own. Please see the details on Sample-spring-hibernate for more on that.

One important thing to note is that if you use Spring transaction management, it is best to implement transaction demarcation using Spring as well. That is, if you use native JTA interfaces, i.e the UserTransaction object to define begin and end of a transaction, Spring's transaction-scope resource tracking may not function and for example Entity Manager instances provided by Spring will be non-transactional (that is, you will get a new one on every invocation and there will be no managed entities afterwards).

You can bridge the gap via plain JTA transaction demarcation and Spring's implementation also programmatically. More about that in particular here programmatic transaction management with Spring

Using Spring data sources and the Spring transaction manager

If you come from a Spring background, you will most likely be familiar to using Spring configured data sources and in particular the Spring transaction manager (e.g. org.springframework.orm.jpa.JpaTransactionManager).

You are welcome to continue doing so and the same modularization features that apply to all other approaches above apply here as well.

Please check out the sample Sample-springds-hibernate for a hands-on example.

Integration with Jetty

The Jetty Web Container that comes with Z2 can be configured to integrate with a transaction manager (see e.g. Jetty JNDI). The most notable impact is that you can use the JNDI name "java:comp/UserTransaction" to retrieve the UserTransaction interface from within the Web application. Note: This lookup is valid as long as the current thread's context class loader is the class loader of the Web application. Practically speaking this means it is safe to rely on this name while within the code of the Web application. When switching threads or invoking modules that may for some reason switch the context class loader or when invoked from another entry point to your application this may not be the case.

When using com.zfabrik.jta, having

  <New id="tx" class="org.eclipse.jetty.plus.jndi.Transaction">
    <Arg>
      <New class="com.zfabrik.tx.UserTransaction"/>
    </Arg>
  </New>

in environment/webServer/jetty.xml does the trick. In other setups this varies. Please check the JTA samples mentioned above for an example of another configuration.

Summary

It is not as complex as it sounds. This page intentionally goes into some depths to give an understanding on a subject that often is ignored until failing, which is exactly when it is late to understand and correct assumptions. It will be best to pick one of the samples that best fits to your situation and take it from there.

Appendix

Other entry points

When your code is not invoked from a Web application, but for example when you call services of yours based on a timer thread, or some scheduler library, or some other protovol implementation, in that case you need to prepare all the context that is prepared by a Web container (for example) otherwise. I.e. you are entering the container business. That is not so bad. It's actually great news that you can do that. Once we have an appropriate Wiki page in place for that and the Sample-quartz-spring-worker-process-TBD in place there will be more details.

In short: The skeleton to prepare Unit of Work, Context Classloader, as well as the Transaction scope looks something like this:

ApplicationThreadPool.instance().executeAs(new Callable<Void>() {
    public Void call() throws Exception {
      return ThreadUtil.cleanContextExceptionExecute(
        classloader,
        new Callable<Void>() {
          public Void call() throws Exception {
            return TransactionUtil.run(userTransaction, theActualCallable); 
          }
        }
      );
    }
  },
  false
);

Updated by Henning Blohm almost 10 years ago · 29 revisions