transaction handling problem

Help
King Chau
2005-07-30
2013-04-08
  • King Chau
    King Chau
    2005-07-30

    I found that every method gets it's own connection from the datasource, how can we handle a transaction for several update?
    sample code?
    possible now?
    e.g.
    tx.begin();
    accountA.balance = accountA.balance - 500
    accountDao.update(accountA); '  - 500
    accountB.balance = accountB.balance + 500
    accountDao.update(accountB); ' + 500
    tx commit();

     
    • King Chau
      King Chau
      2005-07-30

    • Tom
      Tom
      2005-07-30

      Transactions need to be defined within the same connection.  MDAOG assumes connection pooling.  Some database drivers provide an XADataSource object that can manage transactions that are distributed over pooled connections.  PostgreSQL does not provide an XADataSource last time I checked.  That would be the easy fix.

      In the meantime you can create your own method in the DAO object to handle the transaction logic but you'll be writing a bit of JDBC code.  So if you're app only needs transactions in a couple of places you can work it out.  If it needs a lot of different transaction code then you'll be writing a lot of JDBC code which defeats the purpose of using MDAOG.

      There's probably a way to pass around connections back and forth to handle this when you want to use transactions but that seems like a weird and unsafe way to do it.  It's much nicer if the datasource can handle it.

      If you're using oracle, and it seems some people got it working with oracle then i believe oracle implemented the XA API.  Some app servers may also have a wrapper XADataSource.

      There would still need to be some modifications to handle transactions within MDAOG to handle the transaction code.

      If you want to try and work something out for it feel free to try and let me know what you came up with.  I don't have an urgent need for transaction support right now and I had some family issues in the past month... hell, for the past year and a half really and don't know when I'll be able to add transaction support.

      Like I said, if you're only using it in one or two methods, you might be better off just creating your own method in AccountDaoImpl.java and write the jdbc code to handle the transaction.  That has worked for me.

       
    • King Chau
      King Chau
      2005-08-01

      tx.begin();
      Order.insert(...);
      OrderItem.insert(...);
      tx commit();
      ??

      good luck and take care!

       
      • Tom
        Tom
        2005-08-01

        I meant that MDAOG is open source, if you want to try and make the changes you suggested to MDAOG feel free and let me know.

        Thanks

         
    • King Chau
      King Chau
      2005-08-01