Java Code Examples for java.sql.DatabaseMetaData#supportsSavepoints()

The following examples show how to use java.sql.DatabaseMetaData#supportsSavepoints() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JdbcDatabaseConnection.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Override
public Savepoint setSavePoint(String name) throws SQLException {
	if (supportsSavePoints == null) {
		DatabaseMetaData metaData = connection.getMetaData();
		supportsSavePoints = metaData.supportsSavepoints();
		logger.trace("connection supports save points is {}", supportsSavePoints);
	}
	if (supportsSavePoints) {
		Savepoint savepoint = connection.setSavepoint(name);
		logger.trace("save-point {} set with name {}", savepoint, name);
		return savepoint;
	} else {
		return null;
	}
}
 
Example 2
Source File: ConnectionTest.java    From r-course with MIT License 4 votes vote down vote up
/**
 * Tests the savepoint functionality in MySQL.
 * 
 * @throws Exception
 *             if an error occurs.
 */
public void testSavepoint() throws Exception {
    DatabaseMetaData dbmd = this.conn.getMetaData();

    if (dbmd.supportsSavepoints()) {
        System.out.println("Testing SAVEPOINTs");

        try {
            this.conn.setAutoCommit(true);

            createTable("testSavepoints", "(field1 int)", "InnoDB");

            // Try with named save points
            this.conn.setAutoCommit(false);
            this.stmt.executeUpdate("INSERT INTO testSavepoints VALUES (1)");

            Savepoint afterInsert = this.conn.setSavepoint("afterInsert");
            this.stmt.executeUpdate("UPDATE testSavepoints SET field1=2");

            Savepoint afterUpdate = this.conn.setSavepoint("afterUpdate");
            this.stmt.executeUpdate("DELETE FROM testSavepoints");

            assertTrue("Row count should be 0", getRowCount("testSavepoints") == 0);
            this.conn.rollback(afterUpdate);
            assertTrue("Row count should be 1", getRowCount("testSavepoints") == 1);
            assertTrue("Value should be 2", "2".equals(getSingleValue("testSavepoints", "field1", null).toString()));
            this.conn.rollback(afterInsert);
            assertTrue("Value should be 1", "1".equals(getSingleValue("testSavepoints", "field1", null).toString()));
            this.conn.rollback();
            assertTrue("Row count should be 0", getRowCount("testSavepoints") == 0);

            // Try with 'anonymous' save points
            this.conn.rollback();

            this.stmt.executeUpdate("INSERT INTO testSavepoints VALUES (1)");
            afterInsert = this.conn.setSavepoint();
            this.stmt.executeUpdate("UPDATE testSavepoints SET field1=2");
            afterUpdate = this.conn.setSavepoint();
            this.stmt.executeUpdate("DELETE FROM testSavepoints");

            assertTrue("Row count should be 0", getRowCount("testSavepoints") == 0);
            this.conn.rollback(afterUpdate);
            assertTrue("Row count should be 1", getRowCount("testSavepoints") == 1);
            assertTrue("Value should be 2", "2".equals(getSingleValue("testSavepoints", "field1", null).toString()));
            this.conn.rollback(afterInsert);
            assertTrue("Value should be 1", "1".equals(getSingleValue("testSavepoints", "field1", null).toString()));
            this.conn.rollback();

            this.conn.releaseSavepoint(this.conn.setSavepoint());
        } finally {
            this.conn.setAutoCommit(true);
        }
    } else {
        System.out.println("MySQL version does not support SAVEPOINTs");
    }
}
 
Example 3
Source File: ConnectionTest.java    From Komondor with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Tests the savepoint functionality in MySQL.
 * 
 * @throws Exception
 *             if an error occurs.
 */
public void testSavepoint() throws Exception {
    DatabaseMetaData dbmd = this.conn.getMetaData();

    if (dbmd.supportsSavepoints()) {
        System.out.println("Testing SAVEPOINTs");

        try {
            this.conn.setAutoCommit(true);

            createTable("testSavepoints", "(field1 int)", "InnoDB");

            // Try with named save points
            this.conn.setAutoCommit(false);
            this.stmt.executeUpdate("INSERT INTO testSavepoints VALUES (1)");

            Savepoint afterInsert = this.conn.setSavepoint("afterInsert");
            this.stmt.executeUpdate("UPDATE testSavepoints SET field1=2");

            Savepoint afterUpdate = this.conn.setSavepoint("afterUpdate");
            this.stmt.executeUpdate("DELETE FROM testSavepoints");

            assertTrue("Row count should be 0", getRowCount("testSavepoints") == 0);
            this.conn.rollback(afterUpdate);
            assertTrue("Row count should be 1", getRowCount("testSavepoints") == 1);
            assertTrue("Value should be 2", "2".equals(getSingleValue("testSavepoints", "field1", null).toString()));
            this.conn.rollback(afterInsert);
            assertTrue("Value should be 1", "1".equals(getSingleValue("testSavepoints", "field1", null).toString()));
            this.conn.rollback();
            assertTrue("Row count should be 0", getRowCount("testSavepoints") == 0);

            // Try with 'anonymous' save points
            this.conn.rollback();

            this.stmt.executeUpdate("INSERT INTO testSavepoints VALUES (1)");
            afterInsert = this.conn.setSavepoint();
            this.stmt.executeUpdate("UPDATE testSavepoints SET field1=2");
            afterUpdate = this.conn.setSavepoint();
            this.stmt.executeUpdate("DELETE FROM testSavepoints");

            assertTrue("Row count should be 0", getRowCount("testSavepoints") == 0);
            this.conn.rollback(afterUpdate);
            assertTrue("Row count should be 1", getRowCount("testSavepoints") == 1);
            assertTrue("Value should be 2", "2".equals(getSingleValue("testSavepoints", "field1", null).toString()));
            this.conn.rollback(afterInsert);
            assertTrue("Value should be 1", "1".equals(getSingleValue("testSavepoints", "field1", null).toString()));
            this.conn.rollback();

            this.conn.releaseSavepoint(this.conn.setSavepoint());
        } finally {
            this.conn.setAutoCommit(true);
        }
    } else {
        System.out.println("MySQL version does not support SAVEPOINTs");
    }
}
 
Example 4
Source File: ConnectionTest.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Tests the savepoint functionality in MySQL.
 * 
 * @throws Exception
 *             if an error occurs.
 */
public void testSavepoint() throws Exception {
    DatabaseMetaData dbmd = this.conn.getMetaData();

    if (dbmd.supportsSavepoints()) {
        System.out.println("Testing SAVEPOINTs");

        try {
            this.conn.setAutoCommit(true);

            createTable("testSavepoints", "(field1 int)", "InnoDB");

            // Try with named save points
            this.conn.setAutoCommit(false);
            this.stmt.executeUpdate("INSERT INTO testSavepoints VALUES (1)");

            Savepoint afterInsert = this.conn.setSavepoint("afterInsert");
            this.stmt.executeUpdate("UPDATE testSavepoints SET field1=2");

            Savepoint afterUpdate = this.conn.setSavepoint("afterUpdate");
            this.stmt.executeUpdate("DELETE FROM testSavepoints");

            assertTrue("Row count should be 0", getRowCount("testSavepoints") == 0);
            this.conn.rollback(afterUpdate);
            assertTrue("Row count should be 1", getRowCount("testSavepoints") == 1);
            assertTrue("Value should be 2", "2".equals(getSingleValue("testSavepoints", "field1", null).toString()));
            this.conn.rollback(afterInsert);
            assertTrue("Value should be 1", "1".equals(getSingleValue("testSavepoints", "field1", null).toString()));
            this.conn.rollback();
            assertTrue("Row count should be 0", getRowCount("testSavepoints") == 0);

            // Try with 'anonymous' save points
            this.conn.rollback();

            this.stmt.executeUpdate("INSERT INTO testSavepoints VALUES (1)");
            afterInsert = this.conn.setSavepoint();
            this.stmt.executeUpdate("UPDATE testSavepoints SET field1=2");
            afterUpdate = this.conn.setSavepoint();
            this.stmt.executeUpdate("DELETE FROM testSavepoints");

            assertTrue("Row count should be 0", getRowCount("testSavepoints") == 0);
            this.conn.rollback(afterUpdate);
            assertTrue("Row count should be 1", getRowCount("testSavepoints") == 1);
            assertTrue("Value should be 2", "2".equals(getSingleValue("testSavepoints", "field1", null).toString()));
            this.conn.rollback(afterInsert);
            assertTrue("Value should be 1", "1".equals(getSingleValue("testSavepoints", "field1", null).toString()));
            this.conn.rollback();

            this.conn.releaseSavepoint(this.conn.setSavepoint());
        } finally {
            this.conn.setAutoCommit(true);
        }
    } else {
        System.out.println("MySQL version does not support SAVEPOINTs");
    }
}