jackrabbit-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Vibhu Sharma <vi...@projectoffguard.com>
Subject Re: User Managed Transactions on Tomcat : Please Validate the Solution
Date Tue, 06 Oct 2009 16:03:17 GMT
Hi All,
I have been trying to configure JOTM with Jackrabbit (with
DerbyPersistenceManager) and MySQL 5.0 on Tomcat 6.0.14. I have managed to
achive this and now the Transactions across Jackrabbit and MySQL are atomic.

But the solution (pasted below) is (I feel) a little round about and I donot
know the correctness as I am a newbie to JTA. Please validate if the
approach is correct.

The JOTM configuration is done according to the example at:
http://static.raibledesigns.com/downloads/howto-tomcat-jotm.html

The repository is configured according to Deployment Model 2


/****** imports ********/
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;

import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.Node;
import javax.jcr.SimpleCredentials;

import org.apache.jackrabbit.api.XASession;
import org.objectweb.jotm.datasource.DataSourceFactory;


/******** code starts ******/

Context ctx = new InitialContext();

// JDBC stuff
DataSource ds =
    (DataSource)ctx.lookup("java:comp/env/jdbc/myDB");

UserTransaction ut =
(UserTransaction)ctx.lookup("java:comp/UserTransaction");

System.out.println("<<< beginning the transaction >>>");
ut.begin();

// get DB Connection
 java.sql.Connection conn = ds.getConnection();

//getting repository instance
 Repository repository =
(Repository)ctx.lookup("java:comp/env/jcr/repository");
 Session session = repository.login(new
SimpleCredentials("user","user".toCharArray()));

 // extract TransactionManager from DatasourceFactory.jotm
 TransactionManager tmanager =
DataSourceFactory.jotm.getTransactionManager();
 // extract the current Transaction from TransactionManager
 Transaction trans = tmanager.getTransaction();
 // attach the XAResource from Session to Transaction
 trans.enlistResource(((XASession)session).getXAResource());

 // Perform changes to repository
 Node node = session.getRootNode();
 node.addNode("node_to_be_added","base:dummy");

 // ---------- call save() on Node ---> this is very Important
 // without this the changes don't persist even on transaction.commit()
 node.save();

 // JDBC statements
 Statement stmt = conn.createStatement();
 ResultSet rst =
     stmt.executeQuery("select id, foo from testdata");
 if(rst.next()) {
     foo=rst.getInt(2);
 }
 System.out.println("foo = "+ foo +" (before completion)");

 PreparedStatement pstmt = conn.prepareStatement("update testdata set foo=?
where id=1");
 pstmt.setInt(1,++foo);
 pstmt.executeUpdate();

  if (completion != null && completion.equals("commit")) {
      System.out.println("<<< committing the transaction >>>");
      ut.commit();
  } else {
      System.out.println("<<< rolling back the transaction >>>");
      ut.rollback();
  }

 // we set foo to the value stored in the DB
 rst =
     stmt.executeQuery("select id, foo from testdata");
 if(rst.next()) {
     foo=rst.getInt(2);
 }
 System.out.println("foo = "+ foo +" (after completion)");

 conn.close();

session.logout();

 System.out.println("<<< done >>>");

/******** code ends ******/



thanks,
Vibhu



On Fri, Sep 25, 2009 at 3:41 PM, Vibhu Sharma <vibhu@projectoffguard.com>wrote:

> Hi all,
> My application uses MySQL and Jackrabbit to store data. It uses Tomcat as
> the container and the business logic is wired together with Struts.
> I am looking at using JTA to manage the transactions.
> I have looked into Tyrex (last updated in 2005) and JOTM, out of which I
> think JOTM is the better option(Please guide me if there is something
> better).
>
> I have tried to configure JOTM with Jackrabbit on Tomcat but with no
> success.
> Please can any body point me in the right direction or provide a link.
> So far I have been consulting this link -
> http://jotm.objectweb.org/current/jotm/doc/howto-tomcat-jotm.html
>
> regards
> Vibhu

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message