Transaction problem - ROLLBACK when browser closed

Posted on 2005-04-06
Last Modified: 2013-12-12
Hello all,

I am using the innoDB engine and have build a basic ecommerce site. I start a transaction for each new visiter and when they add items to their cart the qty of the product in the relevent table (product table) is adjusted accordingly. The transaction is only commited when the order is placed.

My problem is that if the user ends the session (by leaving site etc) and has items in their basket (I do not use cookies) I want the MySQL DB to ROLLBACK so that the products that are in the cart have their orignal quanities.

Please can someone help?



P.s the only table that is of type innodb is the product table so a rollback will only affect the relevant bit.
Question by:Beam
    LVL 8

    Accepted Solution

    I think the trouble here is that you have no way of knowing when a user leaves your site (except when pressing an exit-button, but this would lead to an obvious solution...) So what I think you need to do is create a custom session-handler. Here you should register all activities of the client, so that you are certain that the user is still active. Furthermore you would need to write a garbage-collection-script that checks if a session is 'dead': i.e. that the last action of the client is a certain amount of minutes (hours or whatever you want) ago. If this is the case, you reset that particular client with your ROLLBACK, and delete the session from your custom session-database (or file or whatever you use).
    You could use the normal php-session handling also. It will generate the PHPSESSIONID for you, and will handle the session. In addition you could keep track of that session (use session_id()). It would be an add-on to your current code in that way, instead of a rewrite... To register an action of the user, you could put some code in a file that is always included, or make a file that is automatically prepended or appended to your code.

    Hope this will help.
    LVL 10

    Expert Comment

    No comment has been added to this question in more than 21 days,so it is now classified as abandoned..
    I will leave the following recommendation for this question in the Cleanup topic area:
    [Accept  _Marcel_'s comment]

    Any objections should be posted here in the next 4 days. After that time, the question will be closed.

    Kshitij Ahuja
    EE Cleanup Volunteer

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now