Previous Topic

Next Topic

Creating a new table under transaction control

You can add an extra level of data integrity when creating a new table by placing the code to create a table inside a transaction. If the transaction is aborted, the table entry in the database dictionary file is removed, and the table data and index files are deleted from disk.

When a table is created inside a transaction, and until the transaction is committed or aborted, the newly created table must be opened with CTOPEN_EXCLUSIVE mode, otherwise the table open operation will fail. After the transaction is committed the table can be opened in non-exclusive mode.

The code fragment below creates a new table under transaction control. Again no error checking code is included in the example:

/* allocate a new table handle */
hTable = ctdbAllocTable(hDatabase);

/* begin a transaction */
ctdbBegin(hTable);

/* add a field
ctdbAddField(hTable, “Field1”, CT_INTEGER, 4);

/* add another field */
ctdbAddField(hTable, “Field1”, CT_CHAR, 30);

/* create the table */
ctdbCreateTable(hTable, “MyTable”, CTCREATE_NORMAL);

/* commit the transaction */
ctdbCommit(hTable);