Example usage for java.sql Connection TRANSACTION_REPEATABLE_READ

List of usage examples for java.sql Connection TRANSACTION_REPEATABLE_READ

Introduction

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

Prototype

int TRANSACTION_REPEATABLE_READ

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

Click Source Link

Document

A constant indicating that dirty reads and non-repeatable reads are prevented; phantom reads can occur.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    int tx = conn.getMetaData().getDefaultTransactionIsolation();
    String txtxt = null;//from   w  w  w .  j  a  v a 2s .  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);
    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;//w  ww  . jav a 2s  . c o  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:com.alibaba.dubbo.governance.status.DatabaseStatusChecker.java

private String getIsolation(int i) {
    if (i == Connection.TRANSACTION_READ_COMMITTED) {
        return "READ_COMMITTED";
    }/* w  w  w.  j ava  2s. co 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 ww w.j  ava 2s.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:edu.umd.cs.submitServer.servlets.ImportProject.java

/**
 * The doPost method of the servlet. <br>
 * //from w  w  w .j  av  a 2  s  . co m
 * This method is called when a form has its tag value method equals to
 * post.
 * 
 * @param request
 *            the request send by the client to the server
 * @param response
 *            the response send by the server to the client
 * @throws ServletException
 *             if an error occurred
 * @throws IOException
 *             if an error occurred
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Connection conn = null;
    FileItem fileItem = null;
    boolean transactionSuccess = false;
    try {
        conn = getConnection();

        // MultipartRequestFilter is required
        MultipartRequest multipartRequest = (MultipartRequest) request.getAttribute(MULTIPART_REQUEST);
        Course course = (Course) request.getAttribute(COURSE);
        StudentRegistration canonicalStudentRegistration = StudentRegistration.lookupByStudentRegistrationPK(
                multipartRequest.getIntParameter("canonicalStudentRegistrationPK", 0), conn);

        fileItem = multipartRequest.getFileItem();

        conn.setAutoCommit(false);
        /*
         * 20090608: changed TRANSACTION_READ_COMMITTED to
         * TRANSACTION_REPEATABLE_READ to make transaction compatible with
         * innodb in MySQL 5.1, which defines READ_COMMITTED as unsafe for
         * use with standard binary logging. For more information, see:
         * <http
         * ://dev.mysql.com/doc/refman/5.1/en/set-transaction.html#isolevel_read
         * -committed>
         */
        conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

        Project project = Project.importProject(fileItem.getInputStream(), course, canonicalStudentRegistration,
                conn);

        conn.commit();
        transactionSuccess = true;

        String redirectUrl = request.getContextPath() + "/view/instructor/projectUtilities.jsp?projectPK="
                + project.getProjectPK();
        response.sendRedirect(redirectUrl);

    } catch (ClassNotFoundException e) {
        throw new ServletException(e);
    } catch (SQLException e) {
        throw new ServletException(e);
    } finally {
        rollbackIfUnsuccessfulAndAlwaysReleaseConnection(transactionSuccess, request, conn);
    }
}

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  ww  w .j  a va 2 s .  c o  m
    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:edu.umd.cs.submitServer.servlets.UploadTestSetup.java

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // TODO sanity checks on the format of the test setup
    MultipartRequest multipartRequest = (MultipartRequest) request.getAttribute(MULTIPART_REQUEST);
    Connection conn = null;//from w  w w  . ja  v  a  2  s. c  o  m
    FileItem fileItem = null;
    boolean transactionSuccess = false;
    try {
        conn = getConnection();

        fileItem = multipartRequest.getFileItem();
        if (fileItem == null)
            throw new ServletException("fileItem is null; this is not good");

        Project project = (Project) request.getAttribute(PROJECT);
        // could be null
        String comment = multipartRequest.getOptionalCheckedParameter("comment");

        // get size in bytes
        long sizeInBytes = fileItem.getSize();
        if (sizeInBytes == 0) {
            throw new ServletException("Trying upload file of size 0");
        }

        // copy the fileItem into a byte array
        InputStream is = fileItem.getInputStream();
        ByteArrayOutputStream bytes = new ByteArrayOutputStream((int) sizeInBytes);
        IO.copyStream(is, bytes);

        byte[] byteArray = bytes.toByteArray();

        FormatDescription desc = FormatIdentification.identify(byteArray);
        if (desc == null || !desc.getMimeType().equals("application/zip")) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                    "You MUST submit test-setups that are either zipped or jarred");
            return;
        }
        Submission canonicalSubmission = Submission
                .lookupMostRecent(project.getCanonicalStudentRegistrationPK(), project.getProjectPK(), conn);
        // start transaction here
        conn.setAutoCommit(false);
        conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        TestSetup.submit(byteArray, project, comment, conn);
        conn.commit();
        transactionSuccess = true;
        if (canonicalSubmission != null
                && canonicalSubmission.getBuildStatus() != Submission.BuildStatus.BROKEN) {
            WaitingBuildServer.offerSubmission(project, canonicalSubmission);
        }

        String redirectUrl = request.getContextPath() + "/view/instructor/projectUtilities.jsp?projectPK="
                + project.getProjectPK();
        response.sendRedirect(redirectUrl);

    } catch (SQLException e) {
        throw new ServletException(e);
    } finally {
        rollbackIfUnsuccessfulAndAlwaysReleaseConnection(transactionSuccess, request, conn);
        releaseConnection(conn);
        if (fileItem != null)
            fileItem.delete();
    }
}

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

static IsolationLevel mapIsolationLevelFromJdbc(int jdbcLevel) {
    switch (jdbcLevel) {
    case Connection.TRANSACTION_NONE:
    default:/*from w  w w  .j a  v a2s. 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:/*  w ww  .  j  a va2  s .  c  o m*/
    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:com.amazon.carbonado.repo.jdbc.LoggingConnection.java

public void setTransactionIsolation(int level) throws SQLException {
    String levelStr;//from  w  w  w . ja va  2  s .c o  m
    switch (level) {
    default:
        levelStr = String.valueOf(level);
        break;
    case Connection.TRANSACTION_NONE:
        levelStr = "TRANSACTION_NONE";
        break;
    case Connection.TRANSACTION_READ_UNCOMMITTED:
        levelStr = "TRANSACTION_READ_UNCOMMITTED";
        break;
    case Connection.TRANSACTION_READ_COMMITTED:
        levelStr = "TRANSACTION_READ_COMMITTED";
        break;
    case Connection.TRANSACTION_REPEATABLE_READ:
        levelStr = "TRANSACTION_REPEATABLE_READ";
        break;
    case Connection.TRANSACTION_SERIALIZABLE:
        levelStr = "TRANSACTION_SERIALIZABLE";
        break;
    }

    mLog.debug("Connection.setTransactionIsolation(" + levelStr + ')');
    mCon.setTransactionIsolation(level);
}