Results 1 to 8 of 8

Thread: MySQL Multi-User / Table Locking

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    207

    MySQL Multi-User / Table Locking

    Hi guys, Im using MySQL 5.0.

    Here is my two tables.

    Code:
    CREATE TABLE IF NOT EXISTS Invoice
    (
    InvoiceID	             MEDIUMINT	NOT NULL	AUTO_INCREMENT,
    InvoiceDate	DATE		NOT NULL,
    ClientID	             VARCHAR(7)	NOT NULL,
    Total		INTEGER(10,2)	NOT NULL,
    AmountPaid	INTEGER(10,2)	NOT NULL,
    PRIMARY KEY	(InvoiceID),
    FOREIGN KEY	(ClientID)	             REFERENCES	Clients(ClientID)
    );
    
    CREATE TABLE IF NOT EXISTS Transaction
    (
    InvoiceID	             MEDIUMINT	NOT NULL,
    ProductID	VARCHAR(7)	NOT NULL,
    QTY		INTEGER(2)	NOT NULL,
    FOREIGN KEY	(InvoiceID) 	REFERENCES	Invoice(InvoiceID),
    FOREIGN KEY	(ProductID)	REFERENCES	Products(ProductID)
    );
    The purpose of this is to record 1 InvoiceID to refer to multipul transaction items. So heres the problem. What if 2 transactions occured, then the below code will not work.

    Code:
    INSERT INTO Invoice (InvoiceDate, ClientID, Total, AmountPaid)
    VALUES (NOW(), 'OZT0001', 130.00, 50.00);
    
    INSERT INTO Transaction (InvoiceID, ClientID, ProductID, QTY, Total)
    VALUES (LAST_INSERT_ID(), 'RAM0001', 2);
    Is there a better way to construct this or any advise on how to go about this?
    Last edited by TURKINATOR; Oct 3rd, 2007 at 03:50 AM.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    207

    Re: DB Head Scratch

    Heres an idea, why don't you lock the table and then allow the transaction to complete then unlock it for the next invoice to process. Champion.

    Never mind me! Just trying to cover up my stupidity.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    207

    Re: DB Head Scratch

    Ok, here is what I have come up with.

    Code:
    INSERT INTO Invoice (InvoiceDate, ClientID, Total, AmountPaid)
    VALUES (NOW(), 'OZT0001', 130.00, 50.00);
    
    LOCK TABLES Invoice READ, Transaction WRITE;
    
    INSERT INTO Transaction (InvoiceID, ProductID, QTY)
    VALUES (LAST_INSERT_ID(), 'RAM0001', 2);
    
    UNLOCK TABLES;
    
    INSERT INTO Invoice (InvoiceDate, ClientID, Total, AmountPaid)
    VALUES (NOW(), 'OZT0002', 180.00, 20.00);
    
    LOCK TABLES Invoice READ, Transaction WRITE;
    
    INSERT INTO Transaction (InvoiceID, ProductID, QTY)
    VALUES (LAST_INSERT_ID(), 'CPU0001', 3);
    
    UNLOCK TABLES;
    Although this works great, now my question is, what if when someone encounters the locked table for inserting, then that becomes wasted. I know DELAYED is an option, but because I am using LAST_INSERT_ID() I thought that it may be a problem.

    Once again I ask if there is a better way to approch what I am trying to do or anything that may helpme in this situation.

    Thanks in advance.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    207

    Re: DB Head Scratch

    No response? If more information is required I can provide it. MySQL DB is the back-end and VB .NET 2005 is used for the front end.

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: DB Head Scratch

    Maybe we all don't have a lot of experience with MySQL or with MySQL in a multi-user environment.

    If you asked about MS SQL - I could answer your questions

    Maybe edit your first post and change the TITLE to

    MySQL LOCKING with MULTI-USERS

    and see if anyone comes along...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    207

    Re: MySQL Multi-User / Table Locking

    Thanks for the tip. Changed the topic, hope it works.

    So how do you go about it in MS SQL? Create a procedure?

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: MySQL Multi-User / Table Locking

    In MS SQL there is an @@IDENTITY and a SCOPE_IDENTITY function available.

    These return the "last identity value" inserted by the "connection" itself - so multi-user concerns go away.

    No locking required for this - which of course is the best method. Locking causes problems in a multi-user system so it's avoided at all costs.

    Also - look at post #6 in this thread

    http://www.vbforums.com/showthread.p...93#post2771093

    interesting way to do this without an identity column - in a multi-user environment - and without locking.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    207

    Re: MySQL Multi-User / Table Locking

    When I searched up @@IDENTITY with MySQL it is said that MySQL uses the function LAST_INSERT_ID() *Which I was using* and that its based on the connection.

    I put it to the test, opened up my application twice, one each with different user. Inserted into records in app1 then app2. After running the query SELECT LAST_INSERT_ID() I found that it differed from each user.

    What I am getting to is that LAST_INSERT_ID() is based on that users insert and not the database. So, no table locking is required and now my headache is gone.

    I hope this may in future be a lesson for all. Test before questioning.

    Thanks for pointing me in the right direction szlamany

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width