Free Newsletters:
DatabaseDaily  
Database Journal
Search Database Journal:
 
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» DBA Jobs
» Sitemap

News Via RSS Feed


follow us on Twitter





Brocade Doubles Down on 16 Gbps Fibre Channel

Microsoft Wants iOS Apps to Run on WP7

Avaya Debuts New Virtual Services Switch
Database Journal |DBA Support |SQLCourse |SQLCourse2







Technical Specialist – Pre-sales (MA)
Next Step Systems
US-MA-Littleton

Justtechjobs.com Post A Job | Post A Resume

Featured Database Articles

MySQL

September 21, 2004

MySQL Transactions, Part III - BDB Tables, Table locking and Savepoints

By Ian Gilfillan

BDB Transactions

The previous two columns in this series have covered general transactions with InnoDB tables and transaction isolation levels. This month, we look at transactions with other table types, specifically BDB tables, and all other table types, including the default MyISAM table type.

BDB Tables are not that commonly used. Most tables tend to be either the default MyISAM table type, or, for those specifically wanting ACID compliance, InnoDB. BDB (standing for BerkeleyDB) is the poor relation as far as MySQL goes, a table type that precedes InnoDB, and was included for its transactional capability, but which never achieved as much popularity. The BDB format itself is stable (it is supplied by a third party - Sleepycat Software), but the MySQL interface to it is not yet 'production'.

mysql>CREATE TABLE bdb_table(f INT) TYPE = BDB

First up, note a very important difference in the default behavior of BDB tables compared with InnoDB tables. From one connection, run the following:

mysql> BEGIN;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO bdb_table VALUES(1);
Query OK, 1 row affected (0.00 sec)

From a second connection, see what the table contains:

mysql> SELECT * FROM bdb_table;

Note that the connection hangs - it is waiting for the other connection to COMMIT its transaction before returning a result. Remember that InnoDB tables returned a result based upon the data before any other uncommitted transactions had begun. Commit the transaction, and the result appears:

Connection 1:

mysql> COMMIT;
Query OK, 0 rows affected (0.00 sec)

Connection 2:

mysql> SELECT * FROM bdb_table;
+------+
| f    |
+------+
|    1 |
+------+
1 row in set (7.08 sec)

This default behavior could lead to performance problems in your applications, and is one of the reasons for InnoDB's greater popularity.

Autocommit

MySQL's default behavior is to automatically commit statements not explicitly wrapped in a transaction. As we saw here with InnoDB tables, this means any statement that does not have a BEGIN preceding it will be immediately carried out (as if you were using the non-transactional MyISAM table type), as demonstrated below.

From Connection 1:

mysql> INSERT INTO bdb_table VALUES(2);
Query OK, 1 row affected (0.00 sec)

Connection 2:

mysql> SELECT * FROM bdb_table;
+------+
| f    |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.00 sec)

You can circumvent this behavior by setting AUTOCOMMIT to 0, in which case all statements are treated as if a BEGIN statement precedes them, for example:

Connection 1:

mysql> SET AUTOCOMMIT=1;
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT INTO bdb_table VALUES(3);
Query OK, 1 row affected (0.00 sec)

Connection 2:

mysql> SELECT * FROM bdb_table;

Again the thread hangs, waiting for a commit from the first connection.

Connection 1:

mysql> COMMIT;
Query OK, 0 rows affected (0.00 sec)

Connection 2:

mysql> SELECT * FROM bdb_table;
+------+
| f    |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.00 sec)

Of course, BDB tables also use the ROLLBACK statement to return the data back to the status it was in prior to the transaction:

Connection 1:

mysql> BEGIN;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO bdb_table VALUES(4);
Query OK, 1 row affected (0.01 sec)

mysql> ROLLBACK;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT * FROM bdb_table;
+------+
| f    |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.00 sec)

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

MySQL Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 



Latest Forum Threads
MySQL Forum
Topic By Replies Updated
Attendance report Using Mysql pravingate07 0 February 7th, 06:14 AM
Navicat -- import tdetz 0 February 4th, 09:06 AM
inner joins and where nikj12 1 December 18th, 06:16 PM
Advice about software for a total newbie jvocat 2 December 8th, 03:37 PM