Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Transactions spanning database and server upload etc.

  1. #1

    Default Transactions spanning database and server upload etc.

    Hi,

    I am pretty much a beginner with Spring, and I am trying to set up a new architecture with Spring and Hibernate.

    We have one database, but also a server that our clients talk to. For example, they can upload a form to the server and access it later (it's details also get added to the database).

    I would like to combine these two actions - uploading the form to the server, and updating the database - into a single "transaction", but, I am having a particular problem figuring out how to configure Spring in such a case.

    For example, if the form is uploaded ok, but updating the database fails for some reason, how does Spring know how to roll back the uploading of the form to the server ie to call a method that removes it from the server? Do I need to define some kind of transaction manager for the server? If so, any pointers would be appreciated!

    cheers, and apologies if this isn't clear!

    David

  2. #2

    Default

    Just to clarify - I'm trying to use the Declarative Transaction management. Thanks.

  3. #3
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    The file system isn't transactional. So you would need to write code to delete the file if the DB transaction fails. Alternatively, you could consider writing the data to a BLOB in the DB in the same transaction, so that a single Spring tx can span both storing the information about the data and the data itself.

  4. #4

    Default

    Hi Rod,

    Actually our server is seperate, so uploading a form to the server is more than writing to the filesystem - we have to connect to the server, call it's API to upload it etc..

    What I'm trying to get my head around is whether I can write some code to allow Spring to "know what to do" if I need to rollback both the transaction that spans both the db transaction (Hibernate) and my own transaction with the server. eg is there any way to tell Spring to "include this when you rollback a transaction" in a declarative mode?

    Can I set up some kind of transaction manager for the server that Spring can recognize and use? If not, and I have to write it each method to handle a transaction rollback programmatically (soemthing I really want to avoid if possible), do I have to change how I handle Hibernate transactions to combine the two?

    Hope this is at least a little clear as what I'm trying to achieve!

    cheers,

    David

  5. #5
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    Yes, you can attach to the transaction synchronization that Spring is already using. This is how thread bound resources such as Hibernate Session Factories get handled, for example.

    There's no real documentation on doing this, since this is normally used internally, however all the classes in question have pretty extensive JavaDocs, and there is lots of sample code in terms of the various pieces of code that use this mechanism (Hibernate integration, iBatis, JDO, etc.).

    Regards,
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  6. #6

    Default

    Thanks for the suggestion.

    This is my first look at the Spring code - all the transaction stuff is a little overwhelming at first glance!!

    Would anyone be kind enough to give me a brief overview at the code level, and pointers on where I need to insert / extend things to do what I'm trying to do?

    Would really appreciate it...

    cheers,

    David

  7. #7
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    TransactionSynchronizationManager is the basic for synchronization with the current transaction, via thread bound holders for resources.

    Take a look at how it is used for example via SessionFactoryUtils and HibernateInterceptor. SessionFactoryUtils.getSession ultimately ends up registering a new synchronization, where the Hibernate Session is kept in a SessionHolder class, which is registered (indirectly as a an SpringSessionSynchronization class) as a transaction synchronization. This basically gets callbacks to synchronize with events on the transaction. Looking at HibernateInterceptor, you can see how an interceptor can get in on the action. If you look at the invoke method, the call to getSession will cause the binding and synchronization to happen if there is none. The 'else' case handles the situation where the HibernateInterceptor is applied (accidentally) before the transaction interceptor itself, ensuring that the newly created session is at least still bound to the current thread, althoughin this case it will not be also synchronized to the transaction, since there is no synchronization registered in this case, as the tx doesn't even exist yet. However, that will happen later when a session is requested, as SessionFactoryUtils getSession() will notice that the session needs to be registered.

    Hope this helps,

    Regards,
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  8. #8
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    Just a note for anybody watching this topic that I revised the last 3-4 lines of my previous post. The current description is accurate.

  9. #9

    Default

    Working my way slowly through all this...!

    Another question emerges - for just the database stuff, I have a TransactionProxyFactoryBean set up for my Service Object (Manager), linked to a HibernateTransactionManager.

    Now, to handle the database transactions and the server transactions, can I create a ServerTransactionManager and link the 2 together somehow? Or do I need to create a single HibernateAndServerTransactionManager and apply it to my Service object?

    cheers,

    David

  10. #10
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    You should almost never need more than one transaction manager.

    Just transactionally wrap (using TransactionProxyFactoryBean or BeanNameAutoProxyCreator) all the service objects which need to be transactional. There is no issue if they call into each other, either.

    Regards,
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

Similar Threads

  1. Replies: 4
    Last Post: Oct 26th, 2006, 02:20 AM
  2. Replies: 4
    Last Post: Sep 27th, 2005, 11:31 PM
  3. Replies: 2
    Last Post: May 26th, 2005, 02:30 AM
  4. When is remoting not even necessary?
    By general_pattonm in forum Remoting
    Replies: 6
    Last Post: Jan 14th, 2005, 08:16 PM
  5. transactions / sending mails
    By ChristianMuc in forum Data
    Replies: 2
    Last Post: Oct 3rd, 2004, 12:43 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
  •