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

View: New views
18 Messages — Rating Filter:   Alert me  

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

by kakimoto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



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@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

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

by Bogdan Lucaciu-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Jul 6, 2009 at 8:30 AM, <kakimoto@...> 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@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

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

by Tomas Doran :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 6 Jul 2009, at 06:30, kakimoto@... 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@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

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

by kakimoto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@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@...> 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@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

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

by Tomas Doran :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

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

by kakimoto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

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

by kakimoto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

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

by Joel Bernstein :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 6 Jul 2009, at 12:38, kakimoto@... 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@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

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

by Tomas Doran :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 6 Jul 2009, at 12:32, kakimoto@... 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@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

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

by Tomas Doran :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 6 Jul 2009, at 12:38, kakimoto@... 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@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

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

by kakimoto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


>
> 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@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

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

by kakimoto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


>
> 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@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

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

by Bugzilla from arodland@comcast.net :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Monday 06 July 2009 05:37:49 pm kakimoto@... 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@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

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

by J. Shirley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Jul 6, 2009 at 3:36 PM, <kakimoto@...> 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.

あなたは怠け者。

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

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

by kakimoto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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@...
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst@.../
> Dev site: http://dev.catalyst.perl.org/
>
>
>




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

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

by kakimoto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message




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

> On Mon, Jul 6, 2009 at 3:36 PM, <kakimoto@...> 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@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

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

by Ali M. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

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

by Tomas Doran :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

kakimoto@... 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@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/