Building Transactional Classes

Discussions

EJB programming & troubleshooting: Building Transactional Classes

  1. Building Transactional Classes (2 messages)

    Hi,

    I just wanted to know if anyone new how to make an object transactional. For example if I wanted to write a sequence of three strings to a file and if any of them failed the whole lot would be rolled back.

    e.g.
     UserTransaction tx = getUserTransaction();
    try{
       tx.begin();
       MyTransFileWriter mtf = new MyTransFileWriter();
       mtf.insert(s1);
       mtf.insert(s2);
       mtf.insert(s3);
       tx.commit();

    }catch(Exception e){
      tx.rollback();
    }

    Any resources on this would be appreciated.

    Cheers
    Ben
  2. Building Transactional Classes[ Go to top ]

    To put Three String in a file, if your Strings are smaller than the Stream's BufferSize, then changes will affect after calling the close method of the Stream. Otherwise you can read the file after writing your Strings and if you found some of them has failed, then you can remove others from the file.
  3. Building Transactional Classes[ Go to top ]

    Ben,

    Have you considered using a Distributed Caching product in your architecture? Products such as GemFire give you:

    1. Transactional Semantics accross multiple updates into the cache. These could easily be the String objects in your example below.

    2. Fast file persistence or redundant memory-copies (or both), including the handling of rollbacks if some component of the transaction fails.

    Finally, if you ever want to switch the persistence method from a file to a database, you would simply implement a CacheWriter callback with the necessary logic and without the need for any changes the business logic code.

    Cheers,

    Gideon
    GemFire-The Enterprise Data Fabric
    http://www.gemstone.com