Announcement Announcement Module
No announcement yet.
Client Defined Transactions Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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:

    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++)

    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?


  • #2

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


    • #3
      You can always get a PlatformTransactionManager as defined in any example using transactions, for instance:
        <bean id="transactionManager"
          <property name="dataSource">
            <ref local="dataSource"/>
      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).

        PlatformTransactionManager txMan = ....;
        TransactionDefinition txDef = new DefaultTransactionDefinition(...);
        TransactionStatus tx = txMan.getTransaction(txDef);
        try {
          // invoke transactional methods
        } catch (...) {
      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 package.

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


      • #4
        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
          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.