Project

General

Profile

How to Transaction Management » History » Version 27

Henning Blohm, 06.06.2013 12:37

1 10 Henning Blohm
h1. Transaction management in Z2
2 3 Henning Blohm
3 15 Henning Blohm
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.
4 1 Henning Blohm
5 24 Henning Blohm
In short: You can use mostly any way of transaction management you can use in Java elsewhere.
6 12 Henning Blohm
7 26 Henning Blohm
A hint for Spring users: If your focus is on using Spring's built-in transaction management and data source configuration features, skip to [[How_to_Transaction_Management#Using Spring data sources and the Spring transaction manager|Using Spring data sources and the Spring transaction manager]].
8 12 Henning Blohm
9
The following aspects will be discussed:
10
11 27 Henning Blohm
* [[How_to_Transaction_Management#The built-in, non-XA JTA implementation of Z2 (com.zfabrik.jta)|The built-in, non-XA JTA implementation of Z2 (com.zfabrik.jta)]]
12
* [[How_to_Transaction_Management#Controlling transactions in code|Controlling transactions in code]]
13 24 Henning Blohm
* Using Z2 Datasource components
14
* Using Hibernate and JPA with Z2 data sources
15 12 Henning Blohm
* Using a full-blown transaction manager
16 24 Henning Blohm
* Using Spring data sources and the Spring transaction manager
17
* Integration of non-Spring Transaction Managers with Spring
18 12 Henning Blohm
* Integration with Jetty
19
* Integration with Spring
20 18 Henning Blohm
* Appendix
21 12 Henning Blohm
22
h2. The built-in, non-XA JTA implementation of Z2 (com.zfabrik.jta)
23
24
The "z2-base.base":http://redmine.z2-environment.net/projects/z2-base/repository/base repository contains the JTA 1.1 API and an implementation of it in the module *com.zfabrik.jta*. 
25
26
*Note*: This is not an XA-capable transaction manager. This implementation provides no atomic transaction control for more than one transactional resource!
27
28
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.
29
30
Technically the implementation relies on Z2's _Unit of Work_ abstraction (see "Unit of Work and transaction management":http://www.z2-environment.eu/v21doc#Unit%20of%20Work%20and%20transaction%20management). 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.
31
32
The "TransactionManager":http://docs.oracle.com/javaee/6/api/javax/transaction/TransactionManager.html interface, as well as the "UserTransaction":http://docs.oracle.com/javaee/6/api/javax/transaction/UserTransaction.html interface are available from the component *com.zfabrik.jta/userTransaction* and can be looked up via the Z2 core API
33
34
<pre><code class="java">IComponentsLookup.INSTANCE.lookup("com.zfabrik.jta/userTransaction",TransactionManager.class)</code></pre>
35
36
or via JNDI as
37
38
<pre><code class="java">new InitialContext().lookup("components:com.zfabrik.jta/userTransaction?type=javax.transaction.TransactionManager")</code></pre>
39
40
and similarly for the UserTransaction interface.
41
42 19 Henning Blohm
This simple approach to JTA has been used in the samples [[Sample-hibernate-basic]] and [[Sample-spring-hibernate]].
43 12 Henning Blohm
44
h2. Controlling transactions in code
45
46
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:
47
48
<pre><code class="java">
49
UserTransaction ut = (UserTransaction) new InitialContext().lookup( JNDI_NAME );
50
ut.begin();
51
try {
52
  // do some work
53
} catch (Exception e) {
54
  logger.log(Level.WARNING,"Error at transaction boundary. Setting transaction to rollbackonly",e);
55
  ut.setRollbackOnly();
56
  throw e;
57
} finally {
58
  if (ut.getStatus()==Status.STATUS_ACTIVE) {
59
    ut.commit();
60
  } else {
61
    ut.rollback();
62
  }
63
}
64
</code></pre>
65
66
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).
67
68 23 Henning Blohm
Now that we looked at it, typically you do not need to implement this yourself. Here are some alternatives:
69 12 Henning Blohm
70 23 Henning Blohm
* Use "TransactionUtil":http://redmine.z2-environment.net/projects/z2-base/repository/base/revisions/master/entry/com.zfabrik.jta/java/src.api/com/zfabrik/tx/TransactionUtil.java from 
71
*com.zfabrik.jta* that implements just the flow above.
72 1 Henning Blohm
* Use any of the many "Transaction Filter" implementations out there.
73 23 Henning Blohm
74 1 Henning Blohm
If you are using Spring:
75 23 Henning Blohm
* Use Spring's transaction management support including annotation based transaction demarcation, as for example in [[Sample-spring-hibernate]] and [[Sample-jta-spring]] (see also [[How to Spring]]). 
76
77
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.
78
79 12 Henning Blohm
80 24 Henning Blohm
h2. Using Z2 DataSource components
81 12 Henning Blohm
82
A JDBC data source wraps JDBC connections, i.e. actual database connections, for re-use along the control flow. 
83
84
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. 
85
86
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]].
87
88
When using the built-in data source, you can look up the "DataSource":http://docs.oracle.com/javase/6/docs/api/javax/sql/DataSource.html interface via a component lookup, e.g. via JNDI like this:
89
90
<pre><code class="java">(DataSource) new InitialContext().lookup(COMPONENT_NAME?type=javax.sql.DataSource)</code></pre>
91
92 1 Henning Blohm
where *COMPONENT_NAME* has to be substituted by the real name of the data source component.
93
94 19 Henning Blohm
The built-in data sources have been used for example in [[Sample-hibernate-basic]] and [[Sample-spring-hibernate]].
95 12 Henning Blohm
96 15 Henning Blohm
An example definition can be found here: "DB":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-hibernate-basic/revisions/master/entry/com.zfabrik.samples.hibernate-basic.domain/DB.properties.
97
98
The component type *javax.sql.DataSource* is documented here: "DataSources":http://www.z2-environment.eu/v21doc#dataSources.
99 12 Henning Blohm
100 24 Henning Blohm
h2. Using Hibernate and JPA with Z2 data sources
101 12 Henning Blohm
102 13 Henning Blohm
Now you have a JTA implementation in place as well as data sources defined, assuming you want to use the "Java Persistence API":http://de.wikipedia.org/wiki/Java_Persistence_API you buy into yet another layer that needs to be integrated with the transaction management system. 
103
104
In JPA, the equivalent of a data base connection is the "EntityManager":http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html 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":http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManagerFactory.html given a definition of a _Persistence Unit_. We cannot go into all the details here. 
105
106 19 Henning Blohm
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]]). 
107 13 Henning Blohm
108 19 Henning Blohm
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":http://redmine.z2-environment.net/projects/z2-base/repository/base/revisions/master/entry/org.hibernate/java/src.api/com/zfabrik/hibernate/EntityManagerUtil.java of the module *org.hibernate* in *z2-base.base". This is how it is done in [[Sample-hibernate-basic]].
109 13 Henning Blohm
110 19 Henning Blohm
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":http://redmine.z2-environment.net/projects/z2-samples/repository/revisions/master/entry/com.zfabrik.samples.hibernate-basic.domain/java/src.impl/META-INF/persistence.xml of your domain module. For example, the one in [[Sample-hibernate-basic]] looks like this:
111 13 Henning Blohm
112
<pre><code class="xml">
113
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
114 20 Henning Blohm
        <persistence-unit name="thingies" transaction-type="JTA">
115
                <provider>org.hibernate.ejb.HibernatePersistence</provider>
116
                <jta-data-source>components:com.zfabrik.samples.hibernate-basic.domain/DB
117
                </jta-data-source>
118
                <class>com.zfabrik.samples.hibernate_basic.thingies.Thingy</class>
119
                <exclude-unlisted-classes>true</exclude-unlisted-classes>
120
                <properties>
121
                        <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
122
                        <property name="hibernate.hbm2ddl.auto" value="update" />
123
                        <property name="hibernate.jdbc.batch_size" value="10" />
124
                        <property name="hibernate.cache.use_second_level_cache" value="false" />
125
                        <property name="hibernate.transaction.manager_lookup_class" value="com.zfabrik.hibernate.TransactionManagerLookup" />
126
                </properties>
127
        </persistence-unit>
128 13 Henning Blohm
</persistence>
129
</code></pre>
130
131
The noteworthy pieces are 
132
133
# The <jta-data-source/> definition that uses a DataSource JNDI name as discussed above.
134
# The <property name="hibernate.transaction.manager_lookup_class"/> declaration
135
136
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* provides an implementation you can use (it's really simple. See "TransactionManagerLookup":http://redmine.z2-environment.net/projects/z2-base/repository/base/revisions/master/entry/org.hibernate/java/src.api/com/zfabrik/hibernate/TransactionManagerLookup.java).
137 1 Henning Blohm
138
When not using *com.zfabrik.jta*, the value to specify differ of course. See also [[Sample-jta-plain]].
139
140
h2. Using a full-blown transaction manager
141
142
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.
143
144 24 Henning Blohm
h2. Integration of non-Spring Transaction Managers with Spring
145
146
Integration with Spring is yet another subject on its own. Please see the details on [[Sample-spring-hibernate]] for more on that. 
147
148
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).
149
150
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":http://static.springsource.org/spring/docs/3.0.x/reference/transaction.html#transaction-programmatic
151
152
h2. Using Spring data sources and the Spring transaction manager
153
154
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. <code>org.springframework.orm.jpa.JpaTransactionManager</code>).
155
156
You are welcome to continue doing so and the same modularization features that apply to all other approaches above apply here as well.
157
158
Please check out the sample [[Sample-springds-hibernate]] for a hands-on example.
159
160 14 Henning Blohm
h2. Integration with Jetty
161
162
The Jetty Web Container that comes with Z2 can be configured to integrate with a transaction manager (see e.g. "Jetty JNDI":http://docs.codehaus.org/display/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.
163
164
When using *com.zfabrik.jta*, having 
165 1 Henning Blohm
166 14 Henning Blohm
<pre><code class="xml">
167 1 Henning Blohm
  <New id="tx" class="org.eclipse.jetty.plus.jndi.Transaction">
168 14 Henning Blohm
    <Arg>
169 1 Henning Blohm
      <New class="com.zfabrik.tx.UserTransaction"/>
170 21 Henning Blohm
    </Arg>
171
  </New>
172
</code></pre>
173 6 Henning Blohm
174 22 Henning Blohm
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.
175
176 14 Henning Blohm
h2. Summary
177 6 Henning Blohm
178 14 Henning Blohm
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.
179 18 Henning Blohm
180
181
h2. Appendix
182
183
h3. Other entry points
184
185
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. 
186
187
In short: The skeleton to prepare _Unit of Work_, _Context Classloader_, as well as the Transaction scope looks something like this:
188
189
<pre><code class="java">
190
ApplicationThreadPool.instance().executeAs(new Callable<Void>() {
191
    public Void call() throws Exception {
192
      return ThreadUtil.cleanContextExceptionExecute(
193
        classloader,
194
        new Callable<Void>() {
195
          public Void call() throws Exception {
196
            return TransactionUtil.run(userTransaction, theActualCallable); 
197
          }
198
        }
199
      );
200
    }
201
  },
202
  false
203
);
204
</code></pre>
205
206
The details are another story...