Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

MySQL - Procedure and Trigger Question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    MySQL - Procedure and Trigger Question

    Casting a net for ideas as I'm not expert in MySQL:

    TableA has a trigger that, upon an Insert, runs a StoredProcedure1 to update some values in TableB.

    Now, every night I will run StoredProcedure2 that will be inserting records to TableA or, depending on values in TableB, I will need to insert to TableC.

    The problem is that I don't know that I can count on the TableA SP1 to finish it's job updating TableB before the next record is considered in SP2.

    Let me try some cheap psuedo-code of what I'd like to have happen:

    Code:
    Begin_Loop:
    SP2 has a candidate record
    If TableB values are X, then
       insert to TableC
    else
       insert to TableA
    end if
    
    If TableA got the insert then
       SP1 will run, updating TableB
    end if
    
    SP2 now waits for SP1 to finish so that the loop can continue
    and have updated information in TableB, to make the next decision
    
    next record in loop
    This is cheap and simplified, but maybe it'll help. Describing it is actually harder than simple questions of timing etc.

    Thoughts, ideas invited!
    -Steve
    sigpic

    #2
    Re: MySQL - Procedure and Trigger Question

    Steve,
    Not positive that I understand totally, but let me give it a shot.
    When you insert into TableB using SP1, the insert statement will complete after the insertion of the record in TableB, however I'm pretty sure that your existing loop won't be able to see that record if you are using a MySQL cursor.
    Does that help, or am I confused?
    Alan

    Comment


      #3
      Re: MySQL - Procedure and Trigger Question

      Nearly Alan -

      When SP2 inserts to TableA, SP1 updates TableB.

      Now, SP2 actually needs to make a decision about whether to insert to TableA or TableC, but the decision is based on values in TableB.

      We may need to insert an order for 100 widgets in TableA, but only if TableB says there are 100 Widgets available.

      So, let's say SP2 is rolling through a loop (cursor). It considers that it has an order for 100 widgets. It sees by a value in TableB that 100 widgets are indeed available, so it inserts the order for 100 widgets to TableA. Remember that the insert to TableA fires SP1 which updates TableB. For this discussion, let's assume that TableB now shows only 5 widgets remaining (this is not a simple subtraction issue BTW -- the procedure is a bit more involved).

      Now, the next record SP2 is considering might be for 50 widgets. But, if TableB is updated in time, SP2 will decide that it cannot insert an order for 50 widgets to TableA because TableB says there aren't enough, in which case SP2 needs to instead insert the order for 50 widgets to TableC.

      But, my concern and problem at hand is that as SP2 rolls through the records, deciding whether to insert to TableA or TableC, that TableB will not be updated in time for each decision.

      If I was doing this in XBasic, I could have SP2 set a flag that waits for SP1 to complete its job, and then proceed. Not sure if or how that's a good approach in MySQL.
      -Steve
      sigpic

      Comment


        #4
        Re: MySQL - Procedure and Trigger Question

        I believe that TableB will be updated because the update to TableA which fires SP1 which updates TableB will for sure be done before the update to TableA successfully completes.

        Generally what I do is as follows:
        Have an inventory table that has the qty remaining for the widget and have a trigger so it's updated based on values in TableB. If you are using MyISAM tables then I'm sure that when you go back and query to see if have enough inventory, then the value will be updated.
        The only gotcha I know of is that MySQL doesn't like tableA updating TableB via a trigger and then a trigger on TableB trying to update TableA, but I wouldn't think this would be the case here.

        Hopefully this is as clear as mud?? or hopefully gets you closer to your answer.

        Alan

        Comment


          #5
          Re: MySQL - Procedure and Trigger Question

          A trigger in MySQL completes as part of the process that fires the trigger, so it will always be "done" when it says that the first insert is done, there is no waiting.

          Alan

          Comment


            #6
            Re: MySQL - Procedure and Trigger Question

            Ah, so I have nothing to worry about?
            -Steve
            sigpic

            Comment


              #7
              Re: MySQL - Procedure and Trigger Question

              Hi Steve,

              An important question is what database are you using with MySQL? InnoDB, MyISAM? Most likely you are using InnoDB since its the default database but I could be wrong in your case. InnoDB is ACID compliant meaning it will kick off certain components as transactions, triggers being one of them. That means if an insert/update/delete on a table fails, then your trigger will as well. Its basically all or nothing, which what banks use to ensure that accounts stay in synch.


              Store procedures, however, do not act in a transactional fashion - you have to include code that does it. Here is an example below:

              Start of stored procedure

              Your declarations here

              DECLARE EXIT HANDLER
              FOR SQLEXCEPTION, SQLWARNING, NOT FOUND
              ROLLBACK;

              START TRANSACTION;

              "YOUR CODE HERE"



              COMMIT;


              Basically what the code above says is if there is a problem roll back everything, if not, commit everything.

              For what you are trying to do, I would try and use transactions with SP2.

              Here would be the logic..


              Start of loop for SP2.

              First record.

              Begin a transaction

              do the work

              commit the transaction

              next loop.

              The good thing about this is that you know that when the next loop starts, everything that was part of the SP2 has been committed as part of the transaction.

              But one thing is I worry that you might ending up creating deadlocks, since Mysql might lock table B, which might become a problem if table A insert triggers an insert on B.

              You might want to consider redesigning your table setup so that the loop on table B does not result in a possible insert on table b.

              If you loop at one table and made adjustments on other tables, then you can have each loop as transactional and not worry about deadlocking the table you are looping on.

              Hope this helps!

              Comment


                #8
                Re: MySQL - Procedure and Trigger Question

                Thanks Scott and Alan for the lesson today.
                -Steve
                sigpic

                Comment

                Working...
                X