CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • suggestion - db commit in APIs?
Pages: [1]

Author Topic: suggestion - db commit in APIs?  (Read 826 times)

pkraxner

  • I’m new here
  • *
  • Posts: 2
  • Karma: 0
  • CiviCRM version: 4.0
  • CMS version: Drupal 7.0
  • MySQL version: 14.14
  • PHP version: 5.3.x
suggestion - db commit in APIs?
February 25, 2012, 01:10:23 pm
Hello,
I am not very experienced developer (and I am a total newbie in web stateless environment, so maybe there is a technological problem with my suggestion), but I just have a question if it is a good idea to have the db commit included in the api call itself?
Now I am working on a PHP script which reads exported bank statements from file and then creates contributions from it through CiviCRM's API.
I guess that it would be better for me to make just one db commit after all transactions are imported (or rollback if there's some problem in the way) which is more appropriate to my situation. The whole bank file for me is a "transaction" and I want it all in CiviCRM or nothing from it.
I have also one real life example from SAP - they also decided to fix their APIs and left the commit on decision of the developer, I guess it was for similar reasons which I've mentioned above.

Pavel

« Last Edit: February 25, 2012, 01:12:05 pm by pkraxner »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: suggestion - db commit in APIs?
February 25, 2012, 01:52:04 pm
Hi,

I agree it would be helpful. However, I'm afraid that the commit is buried to deep into the callchain that it'd be really complicated to extract and leave as an option.

But haven't looked at it in details recently, so even if we destruct the db object it might still be possible to skip the commit. And mysql can't stack transactions so can't be an option either

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: suggestion - db commit in APIs?
February 25, 2012, 04:23:55 pm

1. CiviCRM does stack transactions :). when the outermost transaction is committed then the mysql commit happens. Any failure in between aborts the entire commit (i.e. failures are not stacked)

2. so u can theorotically do what u want, by starting the transaction before calling the api's and then commiting at the end (or failing)

check CRM/Core/Transaction.php

lobo

A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: suggestion - db commit in APIs?
February 26, 2012, 12:19:35 am
ok, thx for the correction, made an incorrect assumption (mysql doesn't nest transactions but the object layer does add it in civicrm)

Shouldn't we do it when we chain API?
It would add an extra transaction, even if it's a simple call, but looking at the code, doesn't seem it takes a lot of resource.

As for the original question, if you add a
Code: [Select]
    require_once 'CRM/Core/Transaction.php';
    $transaction = new CRM_Core_Transaction( );

before the 1st call and
    $transaction->commit( );
after the last, should work. Would be interested to know if it has an impact on the speed for big imports, could you try both and report?

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

pkraxner

  • I’m new here
  • *
  • Posts: 2
  • Karma: 0
  • CiviCRM version: 4.0
  • CMS version: Drupal 7.0
  • MySQL version: 14.14
  • PHP version: 5.3.x
Re: suggestion - db commit in APIs?
February 26, 2012, 03:13:28 am
Thank you both  :)

About the speed measurements: it will take me some time while I have enough test data and also finish the programming, I am slow:) But in some time I'll go back and post the results of performance tests.

Pavel
« Last Edit: February 26, 2012, 03:15:30 am by pkraxner »

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • suggestion - db commit in APIs?

This forum was archived on 2017-11-26.