transactions-manager.php
980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
class TransactionManager {
private $isTransactionStarted = false; //phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase
private static $instance;
public function start() {
global $wpdb;
if ( ! $this->isTransactionStarted ) {
$wpdb->query( 'BEGIN' );
$this->isTransactionStarted = true;
}
}
public function commit() {
global $wpdb;
global $blclog;
$blclog->debug( 'Starting DB commit.' );
$this->start();
try {
$wpdb->query( 'COMMIT' );
$blclog->debug( 'Commit executed.' );
$this->isTransactionStarted = false;
} catch ( Exception $e ) {
$wpdb->query( 'ROLLBACK' );
$blclog->debug( 'Commit failed; rollback.' );
$this->isTransactionStarted = false;
}
}
//phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
static public function getInstance() {
if ( ! self::$instance ) {
self::$instance = new TransactionManager();
}
return self::$instance;
}
}