Menu

Persistence from within a transaction facade

2006-07-24
2013-04-22
  • Mike Amatruda

    Mike Amatruda - 2006-07-24

    Hi,

    I am trying to play around with the tutorial application that comes bundled with the source, but I am running into a problem. 

    It seems that I am able to read and create persistable objects based on existing records in the database no problem, but for some reason when tying to modify or create objects the changes are not persisted back to the database.

    I wasn't sure if this problem was caused by my code (because I had modified the tutorial code slightly, adding in some extra print statements etc.) so I went back to the site and downloaded the binary version of the tutorial application to test. 

    Unfortunately I ended up seeing the same results. 
    using:
    HelloWorld.exe /create newstar newplanet 0.9

    The tutorial app spits out:
    Created a new star with name 'mike' and created a new planet with name 'newplanet' and rotational period of '0.9'!

    but when I query the database, the new records haven't been persisted where (in my understanding...) they should be.

    Experimenting a little more I noticed that calling PersistenceFacade.Persist() on the objects directly, did properly insert the new records into the tables.

    Perhaps I am missing something, but the tutorial sounds like any persistable object that is referenced, or newly created, should have its modifications persisted to the database as long as these actions occur from within a class with the Transaction attribute specified and the object instance has had its .Manage() function called.

    Any help would be appreciated ! 

    Thanks, and great work!

    -Mike

     
    • Mike Amatruda

      Mike Amatruda - 2006-07-24

      Typo in the above:

      the application did actually spit out the correct star name, on the command line I had tested it with the star name "mike" instead of "newstar"
      before copying that text

       
    • Richard Beauchamp

      Hi Mike,

      Sorry for the delay in my reply. I have been on vacation for the past week and have just returned.

      > but when I query the database,
      > the new records haven't been persisted

      Thanks for finding this issue. Yes, there was a problem with the tutorial client code.

      The problem was that the client was getting a reference to the TutorialFacade class via a (deprecated) call to the constructor as follows:

      // create a new instance of TransactionFacade
      TransactionFacade facade = new TransactionFacade();  <- Incorrect.

      The correct way to get a reference to a service class is via a call to the singleton factory method as follows:

      // get the singleton instance of TransactionFacade
      TransactionFacade facade = TransactionFacade.Instance;

      A call to the 'Instance' method enables the transactional behavior of the service class.

      The tutorial is now fixed and the latest code has been uploaded to the DomainObjects website. You can download the corrected tutorial code here: http://domainobjects.sourceforge.net/code/tutorial.zip

      Thanks again. Best regards,

      Richard

       
    • Mike Amatruda

      Mike Amatruda - 2006-08-15

      Works marvellously now.

      Thanks!

      -Mike

       

Log in to post a comment.