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? 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.
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.
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: gp |
last post by:
******************************************************************************************************************************
Catchable fatal error: Object of class stdClass could not be...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |