473,396 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

PDO: determining if a transaction is active

I am currently working on some code for my CMS that creates a site
folder, then creates all the necessary child folders inside it. The
method that creates folders needs to insert into 2 tables so it wraps
the operation in a transaction. (psuedocode below)

public function createItem ($parent)
{
$this -database -beginTransaction ();
$this -database -query ('INSERT INTO items (param1,
param2 ... ) values (val1, val2 ... )');
$newId = myMethodForGettingInsertId ();
$this -database -query ('INSERT INTO folders (id, param3 ... )
values ($newId, val3 ... )');
$this -database -commit ();
return ($newId);
}

(the real code is obviously a lot more sophisticated and has all the
error checking and rollbacks and what have you but the mock up code
below is clearer regarding the intent of the method)

In my site creating class I have something along the lines of

public function createItem ($parent)
{
$this -database -beginTransaction ();
$newId = parent::createItem ($parent);
parent::createItem ($newId, 'images');
parent::createItem ($newId, 'css');
parent::createItem ($newId, 'other_stuff');
$this -database -commit ();
}

This obviously causes an exception. I can take the transaction code
out of my folder class but the creation of a folder should be atomic.
I could leave it out of the site creation class, but I really do need
site creation to be atomic as well.

Is there a way of determining that I am inside a transaction before
attempting to start one?
Dec 18 '07 #1
3 3095
On Dec 18, 5:31 pm, Gordon <gordon.mc...@ntlworld.comwrote:
I am currently working on some code for my CMS that creates a site
folder, then creates all the necessary child folders inside it. The
method that creates folders needs to insert into 2 tables so it wraps
the operation in a transaction. (psuedocode below)

public function createItem ($parent)
{
$this -database -beginTransaction ();
$this -database -query ('INSERT INTO items (param1,
param2 ... ) values (val1, val2 ... )');
$newId = myMethodForGettingInsertId ();
$this -database -query ('INSERT INTO folders (id, param3 ... )
values ($newId, val3 ... )');
$this -database -commit ();
return ($newId);

}

(the real code is obviously a lot more sophisticated and has all the
error checking and rollbacks and what have you but the mock up code
below is clearer regarding the intent of the method)

In my site creating class I have something along the lines of

public function createItem ($parent)
{
$this -database -beginTransaction ();
$newId = parent::createItem ($parent);
parent::createItem ($newId, 'images');
parent::createItem ($newId, 'css');
parent::createItem ($newId, 'other_stuff');
$this -database -commit ();

}

This obviously causes an exception. I can take the transaction code
out of my folder class but the creation of a folder should be atomic.
I could leave it out of the site creation class, but I really do need
site creation to be atomic as well.

Is there a way of determining that I am inside a transaction before
attempting to start one?
Only if you keep track of it yourself, write your own
beginTransaction() and commit() methods and use a transaction count
variable.

All calls to your own beginTransaction() and commit() methods
increment/decrement the transaction counter respectively. Only the
first call to beginTransaction calls $this->database->beginTransaction
and $this->database->commit() is only called when the transaction
counter reaches 0 again.
Dec 19 '07 #2
On Dec 19, 12:33 pm, Tim Hunt <tim.n.h...@gmail.comwrote:
On Dec 18, 5:31 pm, Gordon <gordon.mc...@ntlworld.comwrote:
I am currently working on some code for my CMS that creates a site
folder, then creates all the necessary child folders inside it. The
method that creates folders needs to insert into 2 tables so it wraps
the operation in a transaction. (psuedocode below)
public function createItem ($parent)
{
$this -database -beginTransaction ();
$this -database -query ('INSERT INTO items (param1,
param2 ... ) values (val1, val2 ... )');
$newId = myMethodForGettingInsertId ();
$this -database -query ('INSERT INTO folders (id, param3 ... )
values ($newId, val3 ... )');
$this -database -commit ();
return ($newId);
}
(the real code is obviously a lot more sophisticated and has all the
error checking and rollbacks and what have you but the mock up code
below is clearer regarding the intent of the method)
In my site creating class I have something along the lines of
public function createItem ($parent)
{
$this -database -beginTransaction ();
$newId = parent::createItem ($parent);
parent::createItem ($newId, 'images');
parent::createItem ($newId, 'css');
parent::createItem ($newId, 'other_stuff');
$this -database -commit ();
}
This obviously causes an exception. I can take the transaction code
out of my folder class but the creation of a folder should be atomic.
I could leave it out of the site creation class, but I really do need
site creation to be atomic as well.
Is there a way of determining that I am inside a transaction before
attempting to start one?

Only if you keep track of it yourself, write your own
beginTransaction() and commit() methods and use a transaction count
variable.

All calls to your own beginTransaction() and commit() methods
increment/decrement the transaction counter respectively. Only the
first call to beginTransaction calls $this->database->beginTransaction
and $this->database->commit() is only called when the transaction
counter reaches 0 again.
Yes, that sounds like a cunning plan. I can add that into my PDO
wrapper easily enough I think. Thanks for the help.
Dec 19 '07 #3

"Gordon" <go**********@ntlworld.comwrote in message
news:94**********************************@t1g2000p ra.googlegroups.com...
On Dec 19, 12:33 pm, Tim Hunt <tim.n.h...@gmail.comwrote:
>On Dec 18, 5:31 pm, Gordon <gordon.mc...@ntlworld.comwrote:
I am currently working on some code for my CMS that creates a site
folder, then creates all the necessary child folders inside it. The
method that creates folders needs to insert into 2 tables so it wraps
the operation in a transaction. (psuedocode below)
public function createItem ($parent)
{
$this -database -beginTransaction ();
$this -database -query ('INSERT INTO items (param1,
param2 ... ) values (val1, val2 ... )');
$newId = myMethodForGettingInsertId ();
$this -database -query ('INSERT INTO folders (id, param3 ... )
values ($newId, val3 ... )');
$this -database -commit ();
return ($newId);
}
(the real code is obviously a lot more sophisticated and has all the
error checking and rollbacks and what have you but the mock up code
below is clearer regarding the intent of the method)
In my site creating class I have something along the lines of
public function createItem ($parent)
{
$this -database -beginTransaction ();
$newId = parent::createItem ($parent);
parent::createItem ($newId, 'images');
parent::createItem ($newId, 'css');
parent::createItem ($newId, 'other_stuff');
$this -database -commit ();
}
This obviously causes an exception. I can take the transaction code
out of my folder class but the creation of a folder should be atomic.
I could leave it out of the site creation class, but I really do need
site creation to be atomic as well.
Is there a way of determining that I am inside a transaction before
attempting to start one?

Only if you keep track of it yourself, write your own
beginTransaction() and commit() methods and use a transaction count
variable.

All calls to your own beginTransaction() and commit() methods
increment/decrement the transaction counter respectively. Only the
first call to beginTransaction calls $this->database->beginTransaction
and $this->database->commit() is only called when the transaction
counter reaches 0 again.

Yes, that sounds like a cunning plan. I can add that into my PDO
wrapper easily enough I think. Thanks for the help.
being careful to note that you can rollback an entire set of transactional
statments on a per-connection basis as well as rolling back individual
statements.
Dec 19 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Bryan J Gudorf | last post by:
PDO, an open source python module for interfacing with RDBMS (SQL databases), has now reached 1.2.0! PDO provides an object oriented API, similar to that of ADO or JDBC, to python developers. PDO...
4
by: Luiz Geron | last post by:
Hi all, I'm testing the PDO wrapper to database modules and I'm wondering how few things like this there are around. My problem, actually, is the paramstyle of modules. I want to use kinterbasdb...
2
by: msnews.microsoft.com | last post by:
Hello, I have the scenario. I m building an application either in asp.net or window application. This application is base on n-tier application model. Let us take example of Northwind Database in...
0
by: tony | last post by:
Hello! Below I have three methods it's SavePost, SavePostSetUpTableFlg and SavePostBlowStepFlg. I want to use transaction to keep the data synchronized. All or nothing must be executed. When...
1
by: FrankEBailey | last post by:
Hi, I have a limited number of ad slots on my website, let's say there are 5. I have lots of people wanting to advertise in those slots, let's say there are 100. They each join the queue for an...
2
by: webcm123 | last post by:
Must I use PDOStatement->setFetchMode() before each fetching data if I want to get only or usually ASSOC arrays? Perhaps, not. foreach( $stmt as $array ) { ... } It's the fastest way for...
1
by: gp | last post by:
****************************************************************************************************************************** Catchable fatal error: Object of class stdClass could not be...
11
by: macca | last post by:
Hi, What should I be using for general MySQL database access? I've been using the traditional mysql extension for ages, but I'm trying to update my style to a more OOP paradigm. I've used...
3
by: FeelLikeANut | last post by:
I have the code below. First there is a transaction where I select data. I wrapped it in an explicit transaction because in my real program I run a couple different selects. Nevertheless, the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.