TomEE/OpenEJB + PostgreSQL XA DataSources

Allan Saddi's projects blog

Saturday, May 26. 2012

TomEE/OpenEJB + PostgreSQL XA DataSources

TomEE 1.0 released recently and due to that, I decided to revisit the land of EJBs once again. (Especially what is known as "EJB 3.1 Lite.") I've been a heavy Spring user both with work projects and personal projects, so EJBs have never really been on my radar. And for whatever reason, I always gravitate toward the transaction manager/database side whenever I try out a new container.

Anyway, setting up TomEE/OpenEJB with local transactions is pretty straightforward. What's problematic (and doesn't seem to be well documented at all) is setting up XA datasources.

There are two major problems:
  1. The XADataSource implementation must also implement DataSource. I looked at 3 drivers: PostgreSQL, H2, and briefly, Oracle 11g. PostgreSQL seems to be the only one whose XADataSource does not implement DataSource.

  2. DBCP-356 and DBCP-358. I'm not sure if they're bugs, but they're certainly oddities.
First the easy case, H2, which isn't plagued by either problem. What really isn't evident is that to set up an XA datasource in TomEE/OpenEJB, you must:
  1. Set JdbcDriver to the XADataSource implementation.

  2. Use Definition to set properties for that XADataSource rather than the usual (JdbcUrl, etc.) However, use Username and Password as normal.
So a minimal XA H2 datasource definition would look something like:

<Resource id="myDataSource" type="DataSource">
  JtaManaged true
  JdbcDriver org.h2.jdbcx.JdbcDataSource
  Definition url=jdbc:h2:MyDataBase
  UserName foobar
  Password hunter2
</Resource>

Fairly easy. And to set more properties in Definition, simply use semi-colons to delimit multiple properties.

Now, to use PostgreSQL with TomEE/OpenEJB you have to first create a dummy class that extends PGXADataSource and implements javax.sql.DataSource. If you're using JDK 6/JDBC4, you will most likely have to implement a few more methods as well. For now, it seems ok if the methods simply throw UnsupportedOperationExceptions or similar, since the PostgreSQL JDBC4 classes do the same.

public class MyPGXADataSource extends PGXADataSource implements DataSource {
    public boolean isWrapperFor(Class<?> arg0) throws SQLException {
       throw new UnsupportedOperationException();
    }
    public <T> T unwrap(Class<T> arg0) throws SQLException {
       throw new UnsupportedOperationException();
    }
}

To solve problem #2, you can either use the PostgreSQL 8.4 driver (which doesn't have the problematic interaction with DBCP), or patch up DBCP 1.4 with the patch I attached to DBCP-356. Once that's done, the PostgreSQL XA datasource definition is pretty simple:

<Resource id="myDataSource" type="DataSource">
  JtaManaged true
  JdbcDriver package.to.MyPGXADataSource
  Definition DatabaseName=foodb;ServerName=mydbhost
  UserName foobar
  Password hunter2
</Resource>

The only caveat is that PGXADataSource does not take a URL property, so you will have to pass the database name, server name, port, etc. as separate properties in Definition.

Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)

No comments

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
spacer

 
 

Links

Software projects

Public Hg Repository

Public Maven Repository

Project Trac Sites

Calendar

spacer February '13
Mon Tue Wed Thu Fri Sat Sun
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28      

Quicksearch

Archives

  • February 2013
  • January 2013
  • December 2012
  • Recent...
  • Older...

Categories

  • spacer General
  • spacer Java
  • spacer Python
  • spacer flannel
  • spacer flup
  • spacer py-lib


All categories

Syndicate This Blog

  • spacer RSS 0.91 feed
  • spacer RSS 1.0 feed
  • spacer RSS 2.0 feed
  • spacer ATOM 0.3 feed
  • spacer ATOM 1.0 feed
  • spacer RSS 2.0 Comments

Blog Administration

Open login screen

Powered by

spacer
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.