On Nov 25, 2009, at 10:15 PM, Stephen Connolly wrote:
How do I configure OpenEJB to use an alternate transaction manager
(e.g. atomikos)?
I'vre tried pre-binding the atomikos transaction manager to
openejb:TransactionManager but the logs do not show any difference.
OpenEJB 3.1
Jetty 6.1.16
Seam 2.0.1.GA
Hibernate 3.3.1.GA
Derby 10.5.3.0_1
I looked around the Atomikos site for a good half hour and couldn't
find any straight answers. Seems they have a unique "transaction
manager" class for just about every integration and no javadoc so it's
nearly impossible to figure out what is the
javax.transaction.TransactionManager impl that should be used.
So... here's a mocked up example.
In a jar in the openejb/lib/ directory, there is a file like so:
META-INF/superbiz/service-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<ServiceJar>
<ServiceProvider
id="FooTransactionManager"
service="TransactionManager"
types="TransactionManager,
javax.transaction.TransactionManager"
factory-name="create"
class-name="org.superbiz.TxManagerFactory">
someString = Blah
someInt = 10
someBoolean = false
</ServiceProvider>
</ServiceJar>
The factory looks like so:
package org.superbiz;
import javax.transaction.TransactionManager;
public class TxManagerFactory {
private String someString;
private int someInt;
private boolean someBoolean;
public void setSomeString(String someString) {
this.someString = someString;
}
public void setSomeInt(int someInt) {
this.someInt = someInt;
}
public void setSomeBoolean(boolean someBoolean) {
this.someBoolean = someBoolean;
}
public TransactionManager create() {
// Use someString, someInt, and someBoolean
// as needed to construct the TransactionManager instance
// and any any objects it requires.
return ...
}
}
And it can be declared in an openejb.xml like so:
<?xml version="1.0" encoding="UTF-8"?>
<openejb>
<TransactionManager id="bar"
provider="superbiz#FooTransactionManager">
someInt = 20
</TransactionManager>
</openejb>
When looking at the Atomikos documentation it seems like they might
also want to control the DataSource creation, in which case you can
use the same technique as above to create a "DataSource" serivce
provider.
META-INF/superbiz/service-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<ServiceJar>
..
<ServiceProvider
id="FooDataSource"
service="Resource"
types="javax.sql.DataSource, DataSource"
factory-name="create"
constructor="JtaManaged"
class-name="org.superbiz.DataSourceFactory">
JtaManaged = true
someInt = 10
someBoolean = false
</ServiceProvider>
</ServiceJar>
And the DataSourceFactory looks like:
package org.superbiz;
import javax.sql.DataSource;
public class DataSourceFactory {
public static DataSource create(boolean managed){
if (managed){
return new BasicManagedDataSource();
} else {
return new BasicDataSource();
}
}
}
In this case there are more properties than the DataSourceFactory
accepts directly, so we'll attempt to "roll them over" and inject them
into the object passed out of the create method: so in the above
example we can presume that BasicDataSource and BasicManagedDataSource
both have a someInt and someBoolean property. If they don't, we'll
simply issue warnings that the properties were ignored.
And these can be declared in the openejb.xml like so:
<?xml version="1.0" encoding="UTF-8"?>
<openejb>
<Resource id="MyManagedDataSource"
provider="superbiz#FooDataSource">
</Resource>
<Resource id="MyUnmanagedDataSource"
provider="superbiz#FooDataSource">
JtaManaged = false
</Resource>
</openejb>
Note, you do not have to support a 'JtaManaged' property on your
DataSource service provider, but if you do you get a lot of extra bang
for your buck as OpenEJB will look for that property when trying to
auto match up DataSources to go with the <jta-data-source> and <non-
jta-data-source> in any persistence.xml
Anyway, I hope this helps. I've heard good things about Atomikos and
would be very interested in seeing it work. If it really is as good
as they say, we might even want to use it as the primary Tx Manager
and DataSource provider in some future release -- or at least make it
so it works easily out of the box.
-David