Results 1 to 5 of 5

Thread: Client Defined Transactions

  1. #1
    Join Date
    Aug 2004
    Posts
    29

    Default Client Defined Transactions

    Hi I have a general problem I am trying to resolve and have got my self all confused so I thought I
    would try to get some help.

    The problem revolves around user-defined transactions. Let me explain this by defining a simplication
    of my application. The 'server' in this example provides 2 services, UserManager and QueryManager.

    UserManager lets say has the following methods:
    - addUser(User)
    - removeUser(String login)

    QueryManager lets say has the following methods:
    - Object[] query(String hqlQuery, Map params);

    As it stands these methods are transactional, using TransactionProxyFactoryBean / HibernateTransactionManager
    I use hibernate to implement my DAO interfaces behind the scene.

    So far so good. Now I want to give users of my server the ability to define their own
    transactions: Here is sort of what I want them to do:

    TransactionManager.beginTransaction();
    Object[] user = queryManager.query("some query that returns a list of user ids I want to delete",params);
    for (int x=0;x<user.length;x++)
    userMan.removeUser(user[x].toString());
    TransactionManager.endTransaction();

    Now I know this example is not very smart, and you may say why not have a userManager.removeUsers(String hqlQuery,Map params), or
    a try/catch around the removeUser etc...
    But the point is I cannot predict what the user will be doing in his transaction, and I thought giving them the ability to define their transactions
    would be useful.

    So what I want to do is to somehow have a TransactionManager class that would open a transaction, (which my service classes and dao hibernate
    classes would participate in) until I closed it.

    Is their an existing way of doing this? Or am I completly off the mark here?

    Thanks,
    Hani

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    Hani,

    What TransactionManager implementation are you using here? Local / JTA? Also, where will "uers' be calling this code? view layer?
    Any error? stackTrace?
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    May 2005
    Location
    San Francisco
    Posts
    61

    Default

    You can always get a PlatformTransactionManager as defined in any example using transactions, for instance:
    Code:
      <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
          <ref local="dataSource"/>
        </property>
      </bean>
    In java, you can get access to such manager (injection or otherwise) and use it to start/end transaction. Of course, a lot will depend on the transaction definitions of the specific methods you will be calling (i.e. if a method specifies "PROPAGATION_REQUIRES_NEW", this will not help).

    Code:
      PlatformTransactionManager txMan = ....;
      ...
      ...
      TransactionDefinition txDef = new DefaultTransactionDefinition(...);
      TransactionStatus tx = txMan.getTransaction(txDef);
      try {
        ...
        // invoke transactional methods
        ...
        txMan.commit(txDef);
      } catch (...) {
        txMan.rollback(txDef);
      }
    This should work, but there're a lot of nuances here, such as proper exception handling, etc.

    Another way is to use TransactionTemplate and TransactionCallback from transaction.support package.

    Code:
      <bean id="txTemplate"
    class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager">
          <ref local="transactionManager"/>
        </property>
      </bean>
    
      public void setTxTemplate(TransactionTemplate txTemplate) {
        this.txTemplate = txTemplate;
      }
    
      ...
      ...
      txTemplate.execute(new TransactionCallback() {
         public Object doInTransaction(TransactionStatus status) {
            // invoke transactional methods
         }
      });

  4. #4
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    does the user work directly on the code w/o the ability to work on Spring configuration? are you doing a framework using Spring? Why not allow the user to directly define TransactionDefinitions using properties (declaratively through XML or programatically)?

  5. #5
    Join Date
    May 2005
    Location
    San Francisco
    Posts
    61

    Default

    As far as I understood Hani is trying to accomplish some sort of Command Pattern implementation within a transaction, i.e. to run undefined commands within a transaction.

Similar Threads

  1. Replies: 5
    Last Post: Mar 8th, 2006, 04:32 AM
  2. Context initialization failed
    By kanonmicke in forum Container
    Replies: 7
    Last Post: Sep 29th, 2005, 12:35 AM
  3. Replies: 3
    Last Post: Jul 14th, 2005, 10:30 AM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •