Interesting hibernate transaction issue

I’m working on a new Grails based project and came across a transaction issue. The application is an ecommerce app and as such the placing of the order has a lot going on that should all reside in a transaction.

Being a Grails app I decided to put the processing inside a transactional service method. So far so good. The flow looks something like this:

  1. Create billing address
  2. Create credit card
  3. Assign billing address to card
  4. Create shipping address
  5. Loop through the cart items and for each item find stock(inventory) to fulfill each
  6. Mark the stock as sold as applied
  7. Add each order item to the order
  8. Assign order to customer
  9. Save order and customer
  10. That is a little oversimplified, but you get the idea. So, what was the problem?

    The problem came in during the select for the stock. This select would cause a hibernate exception about the address being a transient object. Well that made no sense at all.

    At first I started down the road of abandoning the whole transaction thing and doing it manually. Naturally that was ugly and very error prone, so I did some research to find out what the cause might be. Came up empty.

    My gut said that a select should not cause a problem with unsaved data. Then I thought that maybe hibernate was enforcing some sort of isolation level mechanism. Most of us never bother with database isolation levels (and fewer even know what they are sadly enough), but perhaps that was causing it. So what I did was move all the save() calls from the end to where they were being used.

    In other words I called save on the two addresses and the credit card before the loops on getting and assigning stock.

    Worked like a charm. I now have the whole thing in a transaction as it should be.

    No TweetBacks yet. (Be the first to Tweet this post)


    Don’t miss anything, subscribe!

    Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

    Comments

    Hi,

    I don’t understand then line “My gut said that a select should not cause a problem with unsaved data.”.

    What select are you actually doing, I assume a select for the address. If so then then error makes sense as you are trying to query something that only hibernate knows about and has not been sent to the hibernate session or database yet. i.e. If you have not called save then this object is not known to hibernates session.

    As I understand it, and someone please correct me if I’m wrong, the session.save method simple tells hibernate that sometime in the future it wants this object to be created in the database. Various things can happen which prevent this e.g. exception which cause a rollback of the session. In addition there is the session.flush method which sends current updates e.g. insert into …, to the database within the databases transaction. This allows you to query objects, user HSQL or raw queries, you have just created but without actually commiting them to the database until the transaction is complete (These would also be rolled back by the Database transaction in event of an exception).

    However in my experience, again someone please correct me if this is wrong, but you usually get “transient object” exceptions when you have not, or can not, setup the relationships between your object correctly in hibernate configuration. E.g. Using Collections, Set with the correct inverse argument and many to one relationships. Also check that you have the correct cascade parameters as this will cause an transiant objects to be persisted even without a save command for them.

    Example.
    - Retrieve Customer
    - Create Order (No save)
    - Assign Order to Customer
    - Save Customer
    If your Customer has a collection of orders and order has a customer in your hibernate mapping and the orders collection has inverse=true and casade of save-update then the newly created order will be created.

    However if you are expecting Customers to have lots of orders you might not want to load them all in a collection and therefore only have the many-to-one relation on the Order with cascase set to update the customers.

    This post explains it better
    http://blogs.warwick.ac.uk/colinyates/entry/hibernates_bizzare_interpretation/
    especially the summary
    – always provide parent.addChild which updates both ends of the relationship
    – always set inverse to true on bidirectional one–to–many
    – make sure you understand the difference between cascades and inverse ;)

    If you already know all this stuff then ignore this comment but it might be useful for others with similar problems.

    If a woman will begin with ugg bailey button certainties, he shall end in ugg classic cardy doubts,but if he will be content to begin with ugg lo pro button doubts, he shall end in ugg boots certainties. Determined to act ugg knightsbridge Decisivly and to bear the consequences.In this ugg women suede shoes world, hesitation can not access to success. Laziness as a lock,which bolts you out of the storehouse of information and makes you an intellectual starveling. http://www.tangsale.com/

    If a woman will begin with ugg bailey button certainties, he shall end in ugg classic cardy doubts,but if he will be content to begin with ugg lo pro button doubts, he shall end in ugg boots certainties. Determined to act ugg knightsbridge Decisivly and to bear the consequences.In this ugg women suede shoes world, hesitation can not access to success. Laziness as a lock,which bolts you out of the storehouse of information and makes you an intellectual starveling.
    http://www.nike-star-shoes.com

    ugg boots has swept the world, many people start again this heavy ugly boots that do not understand, but it does become more popular. cheap ugg boots onlin discount ugg after the price is reasonable, the model has good-looking, is a good choice. uggs boots something like that a lot of styles of ugg are interested can come take a look. http://www.elegantugg.com/

    1120The mbt walk shoes is part of mbt usa the new line scarpe mbt of mbt shoes which run slightly narrower, and have a slightly different feel. Women mbt shoes have all the benefits of the other styles like encouraging you to carry your weight more efficiently.This cheap mbt shoes help improve posture, reuce back pain, and alleviate joint pressure. http://www.mbtshoessale.com/

    The mbt walk shoes is part of mbt usa the new line scarpe mbt of mbt shoes which run slightly narrower, and have a slightly different feel. Women mbt shoes have all the benefits of the other styles like encouraging you to carry your weight more efficiently.

    There are several Gucci shoes that are considered to be elegant and classic designs.When it comes to footwear for wearingout on the town they have a great collection of suede sandals,satin mules,Gucci pumps and more.

    How do you copy PowerPoint presentations to a DVD?
    this PowerPoint to DVD Converter will be your best choice
    PowerPoint to AVI Converter

    This PowerPoint Video converter can easily convert PowerPoint to video in all popular video and audio formats with excellent quality.
    iPod Ripper provides an ideal solution for you to manage, rip, copy, backup, restore your iPod/iPhone on personal computer no matter iTunes installed or not
    This powerful Capture Streaming Video supports comprehensive video sharing websites, online TV channels, Internet radio station, online movie, live sport, live news report and other streaming video websites
    iPhone To Computer Transfer is a powerful converter, mutually trasferring playlists, songs, pictures between iPhone and computer

    I have to write a research paper on the same subject
    and had to dig a lot of sites on the net to find the info. This article has just been terrific in providing relevant information and I want to thank for sharing this great stuff.

    Your work is very good and I appreciate you and hopping for some more informative posts. thank you for sharing great information to us
    nike air max

    Hi, is there anyone know what kind of shoes is MBTs? I noticed more and more people wearing MBT shoes nowadays when standing and walking. It is very special shoes, you can search online, and you can get more benefits from them. And I still found many online stores sale MBT Shoes, and I still know one site which sell MBT shoes discount, you can buy cheap MBT shoes there, MBT saleon the site. You can get free shipping on all orders. No matter standing and walking, when you wearing MBT shoes, you will know why more and more people bought MBT shoes, any question or concerns please contact us via email.

    It’s not so easy to buy a pretty good custom essays, preferably if you are booked. I advise you to notice buy custom essay papers and to be devoid from doubt that your work will be done by writing services

    On jog of the mill, truthful estate price advance in 32 countries, included in rating, amounts 8,4% during a year. With an internet business you don’t need no span nevertheless for you have youre own product with the purpose of you distribute beside yourself. Finding a possessions to suit could prove grim if you are unaware of the urban’s geography after that what each piece can contain to suggestion your group. Singapore is not only rent office space easiest place on the road to do business in office rental properties planet (according to Doing Business 2009 Report) nevertheless it is besides the most competitive country in Asia (according to the World Economic Forum’s Global Competitiveness Report).

    Wir bieten alle gold wow Dienstleistungen.

    Reebok ZigTech Shoes is hottest new technology from Zig Pulse Shoes. Mens ZigTech Shoes delivers a unique wave of energy that propels you forward with each step, so can reduces stress to your leg muscles by 20% resulting in more endurance for athletes when cross training.

    With the extensive vibram five line-up for 2010, there’s always a pair of barefoot five fingers to match your activities and personality.The year 2010 ushers in more excitement for barefoot runners, athletes and other sports enthusiasts who enjoy physical activities with simulation of walking and running without vibram shoes

    !!!!!http://www.efox-shop.com/ !!!!!!!
    ` ╰—┘ 。 ┅★`_、
    │\__╭╭╭╭╭__/│
    │           │
    │           │
    │ ●       ● │
    │≡    o    ≡│
    │           │
    ╰——┬O◤▽◥O┬——╯
       |  o  |
       |╭---╮| ┌┬┬┬┐ 
    ╞╧╧╧╧══╧╧╧╧╧╧╧╧╡

    Sciphones i68,Ciphone,cect handys,Sciphones,cect handy,Ciphone c6,hiphone,HIPHONE 4,pinphone 3gs,handy shop

    Personne ne peut ignorer l’existence de Christian Louboutin dans la mode du monde. Et ce qui rend si remarquable Chaussures Louboutin ? Sa qualité exquise, son artisanat d’art, ses sexy talons hauts, ses dessins bizarres et bien sûr sa semelle rouge counnue comme le symbole des Chaussures Christian Louboutin.

    Leave a comment

    (required)

    (required)