Announcement Announcement Module
Collapse
No announcement yet.
newbie question about custom transaction manager Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • newbie question about custom transaction manager

    Hi all,

    I created a TransactionManager for X-Hive/DB for which there doesn't (yet) exist an implementation. I used it to experiment with programmatic and declarative transaction management and actually got it to work! At least a transaction is started and commited/rolled back.

    My problem is that I need a reference to be transaction object (XhiveSessionIf) to get access to the database itself. How do I get a reference to the transaction object in a object that uses the XhiveTransactionManager:

    Code:
    public class BlaImpl implements Bla {
    
      public void doSomething() {
        transactionTemplate.execute(new TransactionCallback() {
          public Object doInTransaction(TransactionStatus ts) {
            try {
    
              // need a reference here
    
            } catch (Exception e) {
              ts.setRollbackOnly();
            }
            return null;
          }
        });
      }
    
    }
    Thanks for any help and regards,

    Jeroen

    ps.: the transaction manager implementation:
    Code:
    public class XhiveTransactionManager extends AbstractPlatformTransactionManager {
    
      protected Object doGetTransaction() throws TransactionException {
        XhiveDriverIf driver = XhiveDriverFactory.getDriver("data/XhiveDatabase.bootstrap");
        XhiveSessionIf session = null;
        try {
          if (!driver.isInitialized()) {
            driver.init(1000);
          }
          session = driver.createSession();
          session.join();
          session.connect("Administrator", "secret", "MyDatabase");
        } catch (Exception e) {
          e.printStackTrace();
        }
        return session;
      }
    
      protected void doBegin(Object o, TransactionDefinition transactionDefinition) throws TransactionException {
        ((XhiveSessionIf) o).begin();
      }
    
      protected void doCommit(DefaultTransactionStatus defaultTransactionStatus) throws TransactionException {
        XhiveSessionIf session = (XhiveSessionIf) defaultTransactionStatus.getTransaction();
        session.commit();
      }
    
      protected void doRollback(DefaultTransactionStatus defaultTransactionStatus) throws TransactionException {
        XhiveSessionIf session = (XhiveSessionIf) defaultTransactionStatus.getTransaction();
        session.rollback();
      }
    
    }

  • #2
    I was just browsing through the classes and I think you can do a cast from TransactionStatus to DefaultTransactionStatus and get the transaction reference from there.
    However I don't think your are doing the right thing - normally your code should not care about the transaction itself - that's the job of the TM after all. You should not even try/catch exceptions as they will be intercepted or transalated by the TransactionCallback (that's why you are using it).
    Why don't you access the database through normal jdbc access (which happens in a transactional manner)?

    Comment


    • #3
      Hi costin,

      I think you can do a cast from TransactionStatus to DefaultTransactionStatus and get the transaction reference from there.
      That did work.

      However I don't think your are doing the right thing - normally your code should not care about the transaction itself - that's the job of the TM after all. You should not even try/catch exceptions as they will be intercepted or transalated by the TransactionCallback (that's why you are using it).
      Why don't you access the database through normal jdbc access (which happens in a transactional manner)?
      X-Hive/DB is an XML database and not a relational database and thus has no jdbc access. At some point I really need reference to the transaction because the api of X-Hive/DB simply doesn't give you access to the database any other way.

      Thanks,

      Jeroen

      Comment


      • #4
        Why don't you use then the resource adapter through the JCA architecture. It should provide a clean way to talk with the database.

        Comment


        • #5
          I see it has a JDBC bridge (http://www.x-hive.com/products/db/index.html) but after reading the specs it seems it is just used for importing. Anyway, just wanted to point out the jdbc word on the db site . Maybe there is some sort of JDBC support hidden in there somewhere ..

          Comment


          • #6
            Why don't you use then the resource adapter through the JCA architecture. It should provide a clean way to talk with the database.
            There's a resource adapter for X-Hive/DB, can Spring do anything with it? I'm not working in a J2EE environment.

            I will be looking more into Spring DAO, maybe writing a custom implementation for that will give me a clean way to access the database.

            regards,

            Jeroen

            Comment


            • #7
              Yes there is - checkout the jca package.

              Comment

              Working...
              X