You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by Vibhu Sharma <vi...@projectoffguard.com> on 2009/10/06 18:03:17 UTC

Re: User Managed Transactions on Tomcat : Please Validate the Solution

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 <vi...@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

Re: Class Cast exception

Posted by Alexander Klimetschek <ak...@day.com>.
On Tue, Oct 6, 2009 at 19:38, Phukan, Anit <An...@intuit.com> wrote:
> I get this exception while trying to set property value for a node to an
> object
>
>  java.lang.ClassCastException: package.objectClassname cannot be cast to
> javax.jcr.Value
>
> In my code, I am trying to set the property value this way:
>
> patientRecords.setProperty("PatientObject", (Value)
> persistedObject.getObject());
>
> where patientRecords is a node, and persistedObject is the object

You can't store an arbitrary object as property. It has to be a
javax.jcr.Value, ie. one of it's subclasses or simpler yet, using the
appropriate setProperty(String name, <type> value) method.

However, you can serialize your object and store it as a binary
property using setProperty(String,InputStream) in JCR 1.0 or
setProperty(String,Binary) in JCR 2.0.

Regards,
Alex

-- 
Alexander Klimetschek
alexander.klimetschek@day.com

Class Cast exception

Posted by "Phukan, Anit" <An...@intuit.com>.
Hi,

Could someone help me out with this?

I get this exception while trying to set property value for a node to an
object

 java.lang.ClassCastException: package.objectClassname cannot be cast to
javax.jcr.Value

In my code, I am trying to set the property value this way:

patientRecords.setProperty("PatientObject", (Value)
persistedObject.getObject());


where patientRecords is a node, and persistedObject is the object


Any suggestions would be appreciated.

Thanks
Anit