wc_transaction_query function

Only uses transactions if enabled, or read uncommitted is supported
#9279
This commit is contained in:
Mike Jolley 2015-11-04 14:11:40 +00:00
parent 48f8665e2d
commit 61e746730f
5 changed files with 49 additions and 13 deletions

View File

@ -377,7 +377,7 @@ class WC_API_Orders extends WC_API_Resource {
public function create_order( $data ) {
global $wpdb;
$wpdb->query( 'START TRANSACTION' );
wc_transaction_query( 'start' );
try {
if ( ! isset( $data['order'] ) ) {
@ -482,13 +482,13 @@ class WC_API_Orders extends WC_API_Resource {
do_action( 'woocommerce_api_create_order', $order->id, $data, $this );
$wpdb->query( 'COMMIT' );
wc_transaction_query( 'commit' );
return $this->get_order( $order->id );
} catch ( WC_API_Exception $e ) {
$wpdb->query( 'ROLLBACK' );
wc_transaction_query( 'rollback' );
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}

View File

@ -377,7 +377,7 @@ class WC_API_Orders extends WC_API_Resource {
public function create_order( $data ) {
global $wpdb;
$wpdb->query( 'START TRANSACTION' );
wc_transaction_query( 'start' );
try {
if ( ! isset( $data['order'] ) ) {
@ -482,13 +482,13 @@ class WC_API_Orders extends WC_API_Resource {
do_action( 'woocommerce_api_create_order', $order->id, $data, $this );
$wpdb->query( 'COMMIT' );
wc_transaction_query( 'commit' );
return $this->get_order( $order->id );
} catch ( WC_API_Exception $e ) {
$wpdb->query( 'ROLLBACK' );
wc_transaction_query( 'rollback' );
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}

View File

@ -175,7 +175,7 @@ class WC_Checkout {
try {
// Start transaction if available
$wpdb->query( 'START TRANSACTION' );
wc_transaction_query( 'start' );
$order_data = array(
'status' => apply_filters( 'woocommerce_default_order_status', 'pending' ),
@ -324,11 +324,11 @@ class WC_Checkout {
do_action( 'woocommerce_checkout_update_order_meta', $order_id, $this->posted );
// If we got here, the order was created without problems!
$wpdb->query( 'COMMIT' );
wc_transaction_query( 'commit' );
} catch ( Exception $e ) {
// There was an error adding order data!
$wpdb->query( 'ROLLBACK' );
wc_transaction_query( 'rollback' );
return new WP_Error( 'checkout-error', $e->getMessage() );
}

View File

@ -84,7 +84,8 @@ class WC_CLI_Order extends WC_CLI_Command {
public function create( $__, $assoc_args ) {
global $wpdb;
$wpdb->query( 'START TRANSACTION' );
wc_transaction_query( 'start' );
try {
$porcelain = isset( $assoc_args['porcelain'] );
unset( $assoc_args['porcelain'] );
@ -175,7 +176,7 @@ class WC_CLI_Order extends WC_CLI_Command {
do_action( 'woocommerce_cli_create_order', $order->id, $data );
$wpdb->query( 'COMMIT' );
wc_transaction_query( 'commit' );
if ( $porcelain ) {
WP_CLI::line( $order->id );
@ -183,7 +184,7 @@ class WC_CLI_Order extends WC_CLI_Command {
WP_CLI::success( "Created order {$order->id}." );
}
} catch ( WC_CLI_Exception $e ) {
$wpdb->query( 'ROLLBACK' );
wc_transaction_query( 'rollback' );
WP_CLI::error( $e->getMessage() );
}

View File

@ -904,9 +904,44 @@ function wc_array_cartesian( $input ) {
foreach ( $results[ $result_key ] as $key => $value ) {
$converted_values[ $key ] = array_search( $value, $indexes[ $key ] );
}
$results[ $result_key ] = $converted_values;
}
return $results;
}
/**
* Run a MySQL transaction query, if supported.
* @param string $type start (default), commit, rollback
* @since 2.5.0
*/
function wc_transaction_query( $type = 'start' ) {
global $wpdb;
if ( ! defined( 'WC_USE_TRANSACTIONS' ) ) {
// Try to set isolation level to support dirty reads - if this is unsupported, do not use transactions
$wpdb->hide_errors();
$result = $wpdb->query( 'SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' );
if ( false === $result ) {
define( 'WC_USE_TRANSACTIONS', false );
} else {
define( 'WC_USE_TRANSACTIONS', true );
}
}
if ( WC_USE_TRANSACTIONS ) {
switch ( $type ) {
case 'commit' :
$wpdb->query( 'COMMIT' );
break;
case 'rollback' :
$wpdb->query( 'ROLLBACK' );
break;
default :
$wpdb->query( 'START TRANSACTION' );
break;
}
}
}