Example usage for java.sql Connection TRANSACTION_SERIALIZABLE

List of usage examples for java.sql Connection TRANSACTION_SERIALIZABLE

Introduction

In this page you can find the example usage for java.sql Connection TRANSACTION_SERIALIZABLE.

Prototype

int TRANSACTION_SERIALIZABLE

To view the source code for java.sql Connection TRANSACTION_SERIALIZABLE.

Click Source Link

Document

A constant indicating that dirty reads, non-repeatable reads and phantom reads are prevented.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    int tx = conn.getMetaData().getDefaultTransactionIsolation();
    String txtxt = null;//w  w w.j a  v  a  2s  .com
    switch (tx) {
    case Connection.TRANSACTION_NONE:
        txtxt = "TRANSACTION_NONE";
        break;
    case Connection.TRANSACTION_READ_COMMITTED:
        txtxt = "TRANSACTION_READ_COMMITTED";
        break;
    case Connection.TRANSACTION_READ_UNCOMMITTED:
        txtxt = "TRANSACTION_READ_UNCOMMITTED";
        break;
    case Connection.TRANSACTION_REPEATABLE_READ:
        txtxt = "TRANSACTION_REPEATABLE_READ";
        break;
    case Connection.TRANSACTION_SERIALIZABLE:
        txtxt = "TRANSACTION_SERIALIZABLE";
        break;
    default:
        txtxt = "UNKNOWN!!";
    }
    System.out.println(txtxt);
    conn.setTransactionIsolation(tx);
    System.out.println("Done");
    conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
    System.out.println("TX is now " + conn.getTransactionIsolation());
}

From source file:TXInfo.java

public static void main(String[] a) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:MusicVideo");
    int tx = con.getMetaData().getDefaultTransactionIsolation();
    String txtxt = null;/* www  . j a v  a 2 s .  co  m*/
    switch (tx) {
    case Connection.TRANSACTION_NONE:
        txtxt = "TRANSACTION_NONE";
        break;
    case Connection.TRANSACTION_READ_COMMITTED:
        txtxt = "TRANSACTION_READ_COMMITTED";
        break;
    case Connection.TRANSACTION_READ_UNCOMMITTED:
        txtxt = "TRANSACTION_READ_UNCOMMITTED";
        break;
    case Connection.TRANSACTION_REPEATABLE_READ:
        txtxt = "TRANSACTION_REPEATABLE_READ";
        break;
    case Connection.TRANSACTION_SERIALIZABLE:
        txtxt = "TRANSACTION_SERIALIZABLE";
        break;
    default:
        txtxt = "UNKNOWN!!";
    }
    System.out.println(txtxt);
    con.setTransactionIsolation(tx);
    System.out.println("Done");
    con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
    System.out.println("TX is now " + con.getTransactionIsolation());
}

From source file:fr.xebia.springframework.jdbc.ManagedBasicDataSourceTest.java

@Test
public void testDataSource() throws Exception {
    ManagedBasicDataSource managedBasicDataSource = (ManagedBasicDataSource) dataSource;

    Assert.assertEquals("jdbc:h2:mem:dbcp-test", managedBasicDataSource.getUrl());
    Assert.assertEquals("org.h2.Driver", managedBasicDataSource.getDriverClassName());
    Assert.assertEquals("sa", managedBasicDataSource.getUsername());
    Assert.assertEquals(10, managedBasicDataSource.getMaxActive());
    Assert.assertEquals(1, managedBasicDataSource.getMinIdle());
    Assert.assertEquals(Connection.TRANSACTION_SERIALIZABLE,
            managedBasicDataSource.getDefaultTransactionIsolation());
}

From source file:com.gramercysoftware.persistence.dao.GSPDataSourceFactory.java

private void createDataSource() {
    Properties properties = getDataSourceConfig();
    initializeDatabaseDriver(properties);

    ObjectPool connectionPool = createConnectionPool(properties);
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(properties.getProperty("jdbc.url"),
            properties.getProperty("jdbc.user"), properties.getProperty("jdbc.password"));
    new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true,
            Connection.TRANSACTION_SERIALIZABLE);
    dataSource = new PoolingDataSource(connectionPool);
}

From source file:com.alibaba.dubbo.governance.status.DatabaseStatusChecker.java

private String getIsolation(int i) {
    if (i == Connection.TRANSACTION_READ_COMMITTED) {
        return "READ_COMMITTED";
    }//from   w  w w  .  j a va  2  s.  c  o  m
    if (i == Connection.TRANSACTION_READ_UNCOMMITTED) {
        return "READ_UNCOMMITTED";
    }
    if (i == Connection.TRANSACTION_REPEATABLE_READ) {
        return "REPEATABLE_READ";
    }
    if (i == Connection.TRANSACTION_SERIALIZABLE) {
        return "SERIALIZABLE)";
    }
    return "NONE";
}

From source file:JDBCMeta.java

/**
 * Convert a TransactionIsolation int (defined in java.sql.Connection) to
 * the corresponding printable string.//from   w w w  .j  av  a  2  s.c om
 */
public static String transactionIsolationToString(int txisolation) {
    switch (txisolation) {
    case Connection.TRANSACTION_NONE:
        // transactions not supported.
        return "TRANSACTION_NONE";
    case Connection.TRANSACTION_READ_UNCOMMITTED:
        // All three phenomena can occur
        return "TRANSACTION_NONE";
    case Connection.TRANSACTION_READ_COMMITTED:
        // Dirty reads are prevented; non-repeatable reads and
        // phantom reads can occur.
        return "TRANSACTION_READ_COMMITTED";
    case Connection.TRANSACTION_REPEATABLE_READ:
        // Dirty reads and non-repeatable reads are prevented;
        // phantom reads can occur.
        return "TRANSACTION_REPEATABLE_READ";
    case Connection.TRANSACTION_SERIALIZABLE:
        // All three phenomena prvented; slowest!
        return "TRANSACTION_SERIALIZABLE";
    default:
        throw new IllegalArgumentException(txisolation + " not a valid TX_ISOLATION");
    }
}

From source file:fr.xebia.management.config.ManagedBasicDataSourceBeanDefinitionParser.java

@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(ManagedBasicDataSource.class);

    builder.setRole(BeanDefinition.ROLE_APPLICATION);
    builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));

    fillBuilderWithAttributeIfExists(builder, element, "accessToUnderlyingConnectionAllowed",
            "access-to-underlying-connection-allowed", boolean.class, parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "connectionInitSqls", "connection-init-sqls",
            java.util.Collection.class, parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "connectionProperties", "connection-properties",
            String.class, parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "defaultAutoCommit", "default-auto-commit",
            boolean.class, parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "defaultCatalog", "default-catalog", String.class,
            parserContext);//from  w ww. ja v  a  2s.  c om
    fillBuilderWithAttributeIfExists(builder, element, "defaultReadOnly", "default-read-only", boolean.class,
            parserContext);

    Element defaultTransactionIsolationElement = DomUtils.getChildElementByTagName(element,
            "default-transaction-isolation");
    if (defaultTransactionIsolationElement != null) {
        int defaultTransactionIsolation;
        String value = defaultTransactionIsolationElement.getAttribute("value");
        if ("NONE".equals(value)) {
            defaultTransactionIsolation = Connection.TRANSACTION_NONE;
        } else if ("READ_UNCOMMITTED".equals(value)) {
            defaultTransactionIsolation = Connection.TRANSACTION_READ_UNCOMMITTED;
        } else if ("READ_COMMITTED".equals(value)) {
            defaultTransactionIsolation = Connection.TRANSACTION_READ_COMMITTED;
        } else if ("REPEATABLE_READ".equals(value)) {
            defaultTransactionIsolation = Connection.TRANSACTION_REPEATABLE_READ;
        } else if ("SERIALIZABLE".equals(value)) {
            defaultTransactionIsolation = Connection.TRANSACTION_SERIALIZABLE;
        } else {
            String msg = "Invalid value for '<default-transaction-isolation' value=\"" + value + "\" />'";
            parserContext.getReaderContext().fatal(msg, defaultTransactionIsolationElement);
            throw new IllegalStateException(msg);
        }
        builder.addPropertyValue("defaultTransactionIsolation", defaultTransactionIsolation);

    }

    fillBuilderWithAttributeIfExists(builder, element, "driverClassName", "driver-class-name", String.class,
            parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "initialSize", "initial-size", int.class, parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "logAbandoned", "log-abandoned", boolean.class,
            parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "maxActive", "max-active", int.class, parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "maxIdle", "max-idle", int.class, parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "maxOpenPreparedStatements",
            "max-open-prepared-statements", int.class, parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "maxWait", "max-wait", long.class, parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "minEvictableIdleTimeMillis",
            "min-evictable-idle-time-millis", long.class, parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "minIdle", "min-idle", int.class, parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "numTestsPerEvictionRun", "num-tests-per-eviction-run",
            int.class, parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "password", "password", String.class, parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "poolPreparedStatements", "pool-prepared-statements",
            boolean.class, parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "removeAbandoned", "remove-abandoned", boolean.class,
            parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "removeAbandonedTimeout", "remove-abandoned-timeout",
            int.class, parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "testOnBorrow", "test-on-borrow", boolean.class,
            parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "testOnReturn", "test-on-return", boolean.class,
            parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "testWhileIdle", "test-while-idle", boolean.class,
            parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "timeBetweenEvictionRunsMillis",
            "time-between-eviction-runs-millis", long.class, parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "url", "url", String.class, parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "username", "username", String.class, parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "validationQuery", "validation-query", String.class,
            parserContext);
    fillBuilderWithAttributeIfExists(builder, element, "validationQueryTimeout", "validation-query-timeout",
            int.class, parserContext);

    builder.setDestroyMethodName("close");

    return builder.getBeanDefinition();
}

From source file:com.amazon.carbonado.repo.jdbc.JDBCRepository.java

static IsolationLevel mapIsolationLevelFromJdbc(int jdbcLevel) {
    switch (jdbcLevel) {
    case Connection.TRANSACTION_NONE:
    default://  ww w  . j  ava2s  .c o  m
        return IsolationLevel.NONE;
    case Connection.TRANSACTION_READ_UNCOMMITTED:
        return IsolationLevel.READ_UNCOMMITTED;
    case Connection.TRANSACTION_READ_COMMITTED:
        return IsolationLevel.READ_COMMITTED;
    case Connection.TRANSACTION_REPEATABLE_READ:
        return IsolationLevel.REPEATABLE_READ;
    case Connection.TRANSACTION_SERIALIZABLE:
        return IsolationLevel.SERIALIZABLE;
    }
}

From source file:com.amazon.carbonado.repo.jdbc.JDBCRepository.java

static int mapIsolationLevelToJdbc(IsolationLevel level) {
    switch (level) {
    case NONE://from   ww  w . j  av a2  s  .  c om
    default:
        return Connection.TRANSACTION_NONE;
    case READ_UNCOMMITTED:
        return Connection.TRANSACTION_READ_UNCOMMITTED;
    case READ_COMMITTED:
        return Connection.TRANSACTION_READ_COMMITTED;
    case REPEATABLE_READ:
        return Connection.TRANSACTION_REPEATABLE_READ;
    case SNAPSHOT:
        // TODO: not accurate for all databases.
        return Connection.TRANSACTION_SERIALIZABLE;
    case SERIALIZABLE:
        return Connection.TRANSACTION_SERIALIZABLE;
    }
}

From source file:io.github.retz.db.Database.java

private void init(PoolProperties props, boolean enableJmx) throws IOException, SQLException {
    props.setValidationQuery("select 1;");
    props.setJmxEnabled(enableJmx);//from w w w  . ja  v  a2 s .  co m

    dataSource.setPoolProperties(props);

    try (Connection conn = dataSource.getConnection()) { //pool.getConnection()) {
        conn.setAutoCommit(false);
        DatabaseMetaData meta = conn.getMetaData();

        if (!meta.supportsTransactionIsolationLevel(Connection.TRANSACTION_SERIALIZABLE)) {
            LOG.error("Current database ({}) does not support required isolation level ({})", databaseURL,
                    Connection.TRANSACTION_SERIALIZABLE);
            throw new RuntimeException("Current database does not support serializable");
        }
        maybeCreateTables(conn);
        conn.commit();
    }
}