Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 241099
Next
Alex Hales
  • 0
Alex HalesTeacher
Asked: August 10, 20222022-08-10T00:13:30+00:00 2022-08-10T00:13:30+00:00In: Codeigniter, MySQL, pdo, transactions

codeigniter – How does getting mysql’s last insert ID work with transactions? + transaction questions

  • 0

[ad_1]

To answer your first question…

When using transactions, your queries are executed normally as far as your connection is concerned. You can choose to commit, saving those changes, or rollback, reverting all of the changes. Consider the following pseudo-code:

insert into number(Random_number) values (rand()); 
select Random_number from number where Number_id=Last_insert_id();

//php

if($num < 1)
   $this->db->query('rollback;'); // This number is too depressing.
else
   $this->db->query('commit;'); // This number is just right.

The random number that was generated can be read prior to commit to ensure that it is suitable before saving it for everyone to see (e.g. commit and unlock the row).

If the PDO driver is not working, consider using the mysqli driver. If that is not an option, you can always use the query ‘select last_insert_id() as id;’ rather than the $this->db->insert_id() function.

To answer your second question, if you are inserting or updating data that other models will be updating or reading, be sure to use transactions. For example, if a column ‘Number_remaining’ is set to 1 the following problem can occur.

Person A reads 1
Person B reads 1
Person A wins $1000!
Person A updates 1 to be 0
Person B wins $1000!
Person B updates 0 to be 0

Using transactions in the same situation would yield this result:

Person A starts transaction
Person A reads ‘1’ from
Number_remaining
(The row is now locked if select for update is used)
Person B
attempts to read Number_remaining – forced to wait
Person A wins
$1000
Person A updates 1 to be 0
Person A commits
Person B
reads 0
Person B does not win $1000
Person B cries

You may want to read up on transaction isolation levels as well.

Be careful of deadlock, which can occur in this case:

Person A reads row 1 (select ... for update)
Person B reads row
2 (select ... for update)
Person A attempts to read row 2,
forced to wait
Person B attempts to read row 1, forced to wait
Person A reaches innodb_lock_wait_timeout (default 50sec) and is
disconnected
Person B reads row 1 and continues normally

At the end, since Person B has probably reached PHP’s max_execution_time, the current query will finish executing independently of PHP, but no further queries will be received. If this was a transaction with autocommit=0, the query will automatically rollback when the connection to your PHP server is severed.

[ad_2]

  • 0 0 Answers
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 325 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 295 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 292 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.