Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

how to implement Transactions (over different db tables) in a Catalyst project

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


kakimoto at tpg

Jul 5, 2009, 10:30 PM

Post #1 of 18 (1818 views)
Permalink
how to implement Transactions (over different db tables) in a Catalyst project

hi , everyone,

i m trying to put transactions in place for my catalyst project.

For example, I would like to create a new user subscription.
What this does is,
- create two new Address objects (Address db table) where one's for
billing address and another for main contact.
- create a new User Subscriptions object (and link the two new Address
objects by IDs into attributes, billing_address_id and
main_contact_address_id).


What I have refered to:

http://search.cpan.org/~ribasushi/DBIx-Class-0.08107/lib/DBIx/Class/Manual/Cookbook.pod

http://kobesearch.cpan.org/htdocs/DBIx-Class/DBIx/Class/Storage.html#txn_do
http://kobesearch.cpan.org/htdocs/DBIx-Class/DBIx/Class/Storage.html#txn_begin
http://kobesearch.cpan.org/htdocs/DBIx-Class/DBIx/Class/Storage.html#txn_commit
http://search.cpan.org/~ribasushi/DBIx-Class-0.08107/lib/DBIx/Class/Storage.pm


In the past, when I had to work with just DBI, I would manually start a
transaction and commit it at the end if no exceptions occur. In
the case of the latter, I will call a rollback.

How do I do it here in Catalyst/DbiX::Class land?
I tried to read up the
http://kobesearch.cpan.org/htdocs/DBIx-Class/DBIx/Class/Storage.html#txn_do
doc but I can't see how the example can apply to Catalyst.

It says, "$schema->txn_do($coderef)" to get the schema but in catalyst,
we access the
database tables directly. For example,


my $rs = $c->model('myAppDB::UserSusbcriptions')->search( .. );



I'm a bit confused and have looked in a few places. Can anyone please
educate me?

thank you:)

K.akimoto


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


bogdan at sinapticode

Jul 5, 2009, 11:55 PM

Post #2 of 18 (1761 views)
Permalink
Re: how to implement Transactions (over different db tables) in a Catalyst project [In reply to]

On Mon, Jul 6, 2009 at 8:30 AM, <kakimoto [at] tpg> wrote:


>  How do I do it here in Catalyst/DbiX::Class land?
> I tried to read up the
> http://kobesearch.cpan.org/htdocs/DBIx-Class/DBIx/Class/Storage.html#txn_do
> doc but I can't see how the example can apply to Catalyst.
>
> It says, "$schema->txn_do($coderef)" to get the schema but in catalyst,
> we access the
> database tables directly. For example,
>
>
> my $rs = $c->model('myAppDB::UserSusbcriptions')->search( .. );
>
>
>
> I'm a bit confused and have looked in a few places. Can anyone please
> educate me?

Hello

The info is here http://search.cpan.org/perldoc?Catalyst::Model::DBIC::Schema

# to access schema methods directly:
$c->model('FilmDB')->schema->source(...);

Cheers,
--
Bogdan Lucaciu
http://www.sinapticode.com

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


bobtfish at bobtfish

Jul 6, 2009, 1:18 AM

Post #3 of 18 (1768 views)
Permalink
Re: how to implement Transactions (over different db tables) in a Catalyst project [In reply to]

On 6 Jul 2009, at 06:30, kakimoto [at] tpg wrote:

> In the past, when I had to work with just DBI, I would manually
> start a
> transaction and commit it at the end if no exceptions occur. In
> the case of the latter, I will call a rollback.

This is fairly easy to get wrong - txn_do forces you to have a block
which comprises the transaction scope, or your code won't compile.

It will start a transaction at the start of the block, and commit it
at the end if an exception hasn't been encountered during the
transaction


> How do I do it here in Catalyst/DbiX::Class land?
> I tried to read up the
> http://kobesearch.cpan.org/htdocs/DBIx-Class/DBIx/Class/
> Storage.html#txn_do
> doc but I can't see how the example can apply to Catalyst.
>
> It says, "$schema->txn_do($coderef)" to get the schema but in
> catalyst,
> we access the
> database tables directly. For example,
>

$c->model('myAppDB') gets the schema

>
> I'm a bit confused and have looked in a few places. Can anyone please
> educate me?
>


$c->model('myAppDB')->txn_do( sub {
my $rs = $c->model('myAppDB::Table')->search(..);
# Etc etc
die("Rollback") if $foo; # Exceptions cause rollback
# Etc etc
# If you get here, to the end, transaction is committed for you.
});

Can you please supply a doc patch for one or two of the places you
looked to find this information which will explain it more clearly
for the next person?

Cheers
t0m


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


kakimoto at tpg

Jul 6, 2009, 2:21 AM

Post #4 of 18 (1756 views)
Permalink
Re: how to implement Transactions (over different db tables) in a Catalyst project [In reply to]

hello there, Tomas,

Thank you :)

Yes, you're quite right there:)
Prior to receiving this reply, I actually wrapped my existing code in
the subroutine within an sub for the txn_do.

sub save_complete_records
{
...

eval
{
$c->model('myAppDB')->schema->txn_do( sub
{
...
$c->forward(
'/subscriptions/_save_address',
[
{ 'policy_id' => $policy_id, }
]
);

...
}
if ($@)
{
$c->log->debug(' Exception manually trapped here : ' . $@ );
$c->model('myAppDB')->schema->txn_rollback or die "Cannot ROLL
BACK - Address module";
$c->log->debug(' manually rolled back' );
$c->error(
q{ An error has occured with the listing. If this problem } .
q{persists please contact our helpdesk at
helpdesk [at] insuranceline}.
q{com.nz.}
);
}
}


To test, I purposedly entered a very long string for one of the database
attributes to make the transaction
fail.

I have the following questions:
1) I looked at the terminal with all the debug messages. I noticed that
the exception was caught and eventhough the roll back was done, I do not
see the print outs from the liens within the "if ($@)" (exception
handling) section above. Why is that?

2) I noticed that whilst the rollback was successful. the sequence do
not get rolled back.
IS this desired behaviour of DBIx::Class?

To illustrate, before the exception was caught, an entry was made in
my User_Subscriptions database table. The ID was printed out and I took
note of it.
When the operation was complete, I queried my User_Subscriptions
database table (using psql for postgresql for an entry of the noted ID).
No entry of the noted ID was returned.
I went back to the webpage and entered the form with valid attributes
and yes, all
necessary objects were created in the database backend and the sequence
in the User_Subscriptions database table had increased by 1.

Any ideas, Tomas and everyone? :)


thank you :)


k. akimoto



On Mon, Jul 6th, 2009 at 6:18 PM, Tomas Doran <bobtfish [at] bobtfish> wrote:

> This is fairly easy to get wrong - txn_do forces you to have a block
>
> which comprises the transaction scope, or your code won't compile.
>
> It will start a transaction at the start of the block, and commit it
>
> at the end if an exception hasn't been encountered during the
> transaction
>

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


bobtfish at bobtfish

Jul 6, 2009, 3:56 AM

Post #5 of 18 (1760 views)
Permalink
Re: how to implement Transactions (over different db tables) in a Catalyst project [In reply to]

Tomas Doran wrote:

> $c->model('myAppDB')->txn_do( sub {
> my $rs = $c->model('myAppDB::Table')->search(..);
> # Etc etc
> die("Rollback") if $foo; # Exceptions cause rollback
> # Etc etc
> # If you get here, to the end, transaction is committed for you.
> });

And I'm crap.

You need $c->model('myAppDB')->schema->txn_do( sub {

Cheers
t0m


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


kakimoto at tpg

Jul 6, 2009, 4:32 AM

Post #6 of 18 (1761 views)
Permalink
Re: how to implement Transactions (over different db tables) in a Catalyst project [In reply to]

hi Tomas and everyone:)

I still have some questions as per my prev post. Can you please help me
out? :)



I have the following questions:
1) I looked at the terminal with all the debug messages. I noticed that
the exception was caught and eventhough the roll back was done, I do not
see the print outs from the liens within the "if ($@)" (exception
handling) section above. Why is that?

2) I noticed that whilst the rollback was successful. the sequence do
not get rolled back.
IS this desired behaviour of DBIx::Class?

To illustrate, before the exception was caught, an entry was made in
my User_Subscriptions database table. The ID was printed out and I took
note of it.
When the operation was complete, I queried my User_Subscriptions
database table (using psql for postgresql for an entry of the noted ID).
No entry of the noted ID was returned.
I went back to the webpage and entered the form with valid attributes
and yes, all
necessary objects were created in the database backend and the sequence
in the User_Subscriptions database table had increased by 1.

.....snip.....


> And I'm crap.
>
> You need $c->model('myAppDB')->schema->txn_do( sub {
>
> Cheers


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


kakimoto at tpg

Jul 6, 2009, 4:38 AM

Post #7 of 18 (1752 views)
Permalink
Re: how to implement Transactions (over different db tables) in a Catalyst project [In reply to]

So, in the case of an exception, an automatic rollback will take place.
I have tested this in my application and it seems that way.

Can anyone confirm this?
Thank you:)

> This is fairly easy to get wrong - txn_do forces you to have a block
>
> which comprises the transaction scope, or your code won't compile.
>
> It will start a transaction at the start of the block, and commit it
>
> at the end if an exception hasn't been encountered during the
> transaction
>





_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


joel at fysh

Jul 6, 2009, 5:25 AM

Post #8 of 18 (1753 views)
Permalink
Re: how to implement Transactions (over different db tables) in a Catalyst project [In reply to]

On 6 Jul 2009, at 12:38, kakimoto [at] tpg wrote:

> So, in the case of an exception, an automatic rollback will take
> place.
> I have tested this in my application and it seems that way.

Did you look what the code does?
http://cpansearch.perl.org/src/RIBASUSHI/DBIx-Class-0.08107/lib/DBIx/Class/Storage.pm
and http://cpansearch.perl.org/src/RIBASUSHI/DBIx-Class-0.08107/t/81transactions.t

See sub txn_do. It begins a transaction, runs your coderef inside an
eval, rolls back the transaction if the eval raises an exception.

However, this is NOT a Catalyst question. Please subscribe to the
DBIx::Class users list at:
http://lists.scsys.co.uk/mailman/listinfo/dbix-class/ and ask these
questions there. Many of the subscribers are the same, but many
Catalyst users don't use DBIx::Class, and many knowledgeable DBIC
users are not subscribed here. It's just good hygiene to ask the
questions in the appropriate places.

Questions specific to Catalyst::Model::DBIC::Schema are fine here, but
you must realise that everything after C< $c->model("MyDBICSchema") >
or C< $c->model("MyDBICSchema::MyResultSetClass") > is handled by
DBIx::Class and has nothing to do with Catalyst?

Thanks,

/joel

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


bobtfish at bobtfish

Jul 6, 2009, 1:53 PM

Post #9 of 18 (1730 views)
Permalink
Re: how to implement Transactions (over different db tables) in a Catalyst project [In reply to]

On 6 Jul 2009, at 12:32, kakimoto [at] tpg wrote:

> hi Tomas and everyone:)
>
> I still have some questions as per my prev post. Can you please
> help me
> out? :)

Sorry, this is too far into DBIC land for this list IMO.

Please ask on that list.

Cheers
t0m


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


bobtfish at bobtfish

Jul 6, 2009, 1:55 PM

Post #10 of 18 (1730 views)
Permalink
Re: how to implement Transactions (over different db tables) in a Catalyst project [In reply to]

On 6 Jul 2009, at 12:38, kakimoto [at] tpg wrote:

> So, in the case of an exception, an automatic rollback will take
> place.
> I have tested this in my application and it seems that way.
>
> Can anyone confirm this?

Did you even look for this in the manual?

http://search.cpan.org/~ribasushi/DBIx-Class-0.08107/lib/DBIx/Class/
Storage.pm#txn_do

If not, why not. If so, how is this not clear?

Cheers
t0m


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


kakimoto at tpg

Jul 6, 2009, 3:36 PM

Post #11 of 18 (1743 views)
Permalink
Re: how to implement Transactions (over different db tables) in a Catalyst project [In reply to]

>
> Did you look what the code does?
>
http://cpansearch.perl.org/src/RIBASUSHI/DBIx-Class-0.08107/lib/DBIx/Class/Storage.pm
> and
>
http://cpansearch.perl.org/src/RIBASUSHI/DBIx-Class-0.08107/t/81transactions.t

If the API doc is written well, I suppose one doens't need to look at
the source code (unless forced to).
I must admit that I didn't fallback to looking at the regression test. I
will do so next time:)




> See sub txn_do. It begins a transaction, runs your coderef inside an
>
> eval, rolls back the transaction if the eval raises an exception.

Yes I did but at that point in time, the documentation on txn_do wasn't
that clear. I think someone must have updated it since this question was
raised.




> However, this is NOT a Catalyst question. Please subscribe to the
> DBIx::Class users list at:
> http://lists.scsys.co.uk/mailman/listinfo/dbix-class/ and ask these
>
> questions there. Many of the subscribers are the same, but many
> Catalyst users don't use DBIx::Class, and many knowledgeable DBIC
> users are not subscribed here. It's just good hygiene to ask the
> questions in the appropriate places.

there's somethign wrong with the mailing list for Dbix::Class as I have
not been able to get replies /mails.
I have contacted the admin in the past and never got any reply. I will
retry.
Nevertheless, given that Dbix::Class is of the choice for catalyst, I
figured the grey area here may see my questions lucky enough to have
some replies from the many who use Dbix::Class for their Catalyst apps.





_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


kakimoto at tpg

Jul 6, 2009, 3:37 PM

Post #12 of 18 (1734 views)
Permalink
Re: how to implement Transactions (over different db tables) in a Catalyst project [In reply to]

>
> Did you even look for this in the manual?
>
> http://search.cpan.org/~ribasushi/DBIx-Class-0.08107/lib/DBIx/Class/
>
> Storage.pm#txn_do
>
> If not, why not. If so, how is this not clear?
>


Well, it's really clear now . I suppose someone has updated the
documentation since I posted this thread up.

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


arodland at comcast

Jul 6, 2009, 3:48 PM

Post #13 of 18 (1730 views)
Permalink
Re: how to implement Transactions (over different db tables) in a Catalyst project [In reply to]

On Monday 06 July 2009 05:37:49 pm kakimoto [at] tpg wrote:
> > Did you even look for this in the manual?
> >
> > http://search.cpan.org/~ribasushi/DBIx-Class-0.08107/lib/DBIx/Class/
> > Storage.pm#txn_do
> >
> > If not, why not. If so, how is this not clear?
>
> Well, it's really clear now . I suppose someone has updated the
> documentation since I posted this thread up.
>
That's the same version of the same doc as you mentioned in the first post of
the thread :)

Andrew

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


jshirley at gmail

Jul 6, 2009, 3:54 PM

Post #14 of 18 (1728 views)
Permalink
Re: how to implement Transactions (over different db tables) in a Catalyst project [In reply to]

On Mon, Jul 6, 2009 at 3:36 PM, <kakimoto [at] tpg> wrote:

>
> > However, this is NOT a Catalyst question. Please subscribe to the
> > DBIx::Class users list at:
> > http://lists.scsys.co.uk/mailman/listinfo/dbix-class/ and ask these
> >
> > questions there. Many of the subscribers are the same, but many
> > Catalyst users don't use DBIx::Class, and many knowledgeable DBIC
> > users are not subscribed here. It's just good hygiene to ask the
> > questions in the appropriate places.
>
> there's somethign wrong with the mailing list for Dbix::Class as I have
> not been able to get replies /mails.
> I have contacted the admin in the past and never got any reply. I will
> retry.
> Nevertheless, given that Dbix::Class is of the choice for catalyst, I
> figured the grey area here may see my questions lucky enough to have
> some replies from the many who use Dbix::Class for their Catalyst apps.
>
>

Considering the same people/software/servers run both the DBIC list and the
Catalyst list, I'm going to assume you're both lazy and inept.

Doubly so for re-asking the question immediately on the Catalyst list.

You are not entitled to get free help just because you can compose an email.

$B$"$J$?$OBU$1<T!#(B


kakimoto at tpg

Jul 6, 2009, 4:22 PM

Post #15 of 18 (1719 views)
Permalink
Re: how to implement Transactions (over different db tables) in a Catalyst project [In reply to]

It
(http://search.cpan.org/~ribasushi/DBIx-Class-0.08107/lib/DBIx/Class/Storage.pm#txn_do)
says, "If an exception is caught, a rollback is issued and the
exception is rethrown. If the rollback fails, (i.e. throws
an exception) an exception is thrown that includes a
"Rollback failed" message".
and the example illustrates this. Sorry, after reading it again
after the many responses to this thread, I realised it's ok.

Anyway, my apologies to everyone. My bad.


> That's the same version of the same doc as you mentioned in the first
> post of
> the thread :)
>
> Andrew
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/
>
>
>




_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


kakimoto at tpg

Jul 6, 2009, 6:47 PM

Post #16 of 18 (1726 views)
Permalink
Re: how to implement Transactions (over different db tables) in a Catalyst project [In reply to]

On Tue, Jul 7th, 2009 at 8:54 AM, "J. Shirley" <jshirley [at] gmail> wrote:

> On Mon, Jul 6, 2009 at 3:36 PM, <kakimoto [at] tpg> wrote:
>
> >
there's somethign wrong with the mailing list for Dbix::Class as I have
not been able to get replies /mails. I have contacted the admin in the
past and never got any reply. I will retry.

Sorry if it caused any inconvenience , guys:(

> Considering the same people/software/servers run both the DBIC list
> and the
> Catalyst list, I'm going to assume you're both lazy and inept.
>
> Doubly so for re-asking the question immediately on the Catalyst
> list.
>
> You are not entitled to get free help just because you can compose an
> email.
>
> $B$"$J$?$OBU$1<T!#(B
>




_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


tclwarrior at gmail

Jul 6, 2009, 11:45 PM

Post #17 of 18 (1690 views)
Permalink
Re: how to implement Transactions (over different db tables) in a Catalyst project [In reply to]

Kakimoto

I recommend you read this article
http://blog.urth.org/2009/07/what-is-catalyst-really.html

It might help you understand more when its a Catalyst Issue or a
another component issue!
And help you better trouble shoot your problems.

Apparently Catalyst will help you send a request to the data model and
return the request result to a view
The content of the request and its semantic apparently from this
conversation and the article is the Model responsibility

So if the Model is Transaction capable, you will have transaction!
Can transactions be implemented at the Catalyst/glue level, apparently
Catalyst was not conceived to do so!

I hope this helped!

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


bobtfish at bobtfish

Jul 7, 2009, 2:46 AM

Post #18 of 18 (1673 views)
Permalink
Re: how to implement Transactions (over different db tables) in a Catalyst project [In reply to]

kakimoto [at] tpg wrote:
> Sorry, after reading it again
> after the many responses to this thread, I realised it's ok.

It's _NOT OK_, as you didn't get it first time. So it's obviously not
clear *enough*.

Please supply the DBIC list with a doc patch to make it more clear and
explicit.

Thanks
t0m

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.