Example usage for java.sql Connection TRANSACTION_READ_UNCOMMITTED

List of usage examples for java.sql Connection TRANSACTION_READ_UNCOMMITTED

Introduction

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

Prototype

int TRANSACTION_READ_UNCOMMITTED

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

Click Source Link

Document

A constant indicating that dirty reads, non-repeatable reads and phantom reads can occur.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();

    DatabaseMetaData md = conn.getMetaData();
    System.out.println("supportsTransactionIsolationLevel - "
            + md.supportsTransactionIsolationLevel(Connection.TRANSACTION_READ_UNCOMMITTED));

    conn.close();/*from   w  w w . j a  v a  2 s .  c om*/
}

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  .  java2s .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);
    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  . j  a  v a  2 s.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);
    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";
    }//from w  ww. ja  va  2  s  .  com
    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  a  va2 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   ww  w .j  av  a2  s  . com*/
    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://from  w w  w  .  ja va 2 s .  c om
        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 a 2  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:dao.PendingfriendDaoDb.java

/**
 * This method adds a pending friend as a friend.
 * @param guestLogin - the guest login that is added as a pending friend
 * @param memberId - the memberId to whom the pending friend is added.
 * @param memberLoginInfo - the members login information bean
 * @throws BaseDaoException//from  w  w w  .  j a  v  a2s  . c  o m
 **/
public void addPendingFriend(String guestLogin, String memberId, Hdlogin memberLoginInfo)
        throws BaseDaoException {

    /**
      * (loginId = memberId) (sending person) 
      * (destloginId=guestLogin) (Receiving person, receives the request as a friend)
    */
    if (RegexStrUtil.isNull(guestLogin) || RegexStrUtil.isNull(memberId)) {
        throw new BaseDaoException("params are null");
    }

    /**
     * Get the guest's login information from guestLogin
          * the person sending the request, the order is important, do not remove this
     */
    Hdlogin hdlogin = getLoginid(guestLogin);
    if (hdlogin == null) {
        throw new BaseDaoException("hdlogin is null for guestLogin " + guestLogin);
    }
    String guestId = hdlogin.getValue(DbConstants.LOGIN_ID);
    if (RegexStrUtil.isNull(guestId)) {
        throw new BaseDaoException("guestId is null for guestLogin " + guestLogin);
    }
    String gfname = hdlogin.getValue("fname");
    String glname = hdlogin.getValue("lname");
    String to = hdlogin.getValue("email");

    /**
    * if already a friend, ignore it
    */
    List prefResult = null;
    Object[] myParams = { (Object) guestId, (Object) memberId };

    /**
     *  Get scalability datasource for pendingfriends - not partitioned
     */
    String sourceName = scalabilityManager.getWriteZeroScalability();
    ds = scalabilityManager.getSource(sourceName);
    if (ds == null) {
        throw new BaseDaoException("ds null, addPendingfriend() " + sourceName);
    }
    String from = webconstants.getMailfrom();

    /**
    * check if this entry exists for this user, do read_uncommitted also
    */
    HashSet pendingSet = null;
    Connection conn = null;
    try {
        conn = ds.getConnection();
        if (conn != null) {
            conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
            pendingSet = listQuery.run(conn, memberId, guestId);
            conn.close();
        }
    } catch (Exception e) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e1) {
            throw new BaseDaoException("connection close exception ", e1);
        }

        throw new BaseDaoException("error occured in db query", e);
    }

    /**
    * this entry already exists in the pendinglist, return
    */
    if (!pendingSet.isEmpty()) {
        Iterator it1 = pendingSet.iterator();
        Integer count = new Integer(((Pendingfriend) it1.next()).getValue("count(*)"));
        if (count > 0) {
            return;
        }
    }

    /**
     *  add this friend
     */
    try {
        addQuery.run(guestId, memberId);
    } catch (Exception e) {
        throw new BaseDaoException("error occured while adding a PendingfriendAddQuery()" + addQuery.getSql()
                + ", guestId = " + guestId + ", memberId = " + memberId, e);
    }

    /**
     * If the Email "to" field is missing, don't send email, just return
     */
    if (!RegexStrUtil.isNull(to)) {

        /**
         *  Get scalability datasource for hdprofile - partitioned on loginId
         */
        sourceName = scalabilityManager.getReadScalability(guestId);
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, addPendingfriend() " + sourceName);
        }

        /**
         * check if the guest needs to be notified
         */
        Object[] params = { (Object) guestId };
        //params[0] = guestId;
        prefResult = null;
        try {
            prefResult = myprofileQuery.execute(params);
        } catch (Exception e) {
            throw new BaseDaoException("error occured while executing HdprofileQuery()"
                    + myprofileQuery.getSql() + " guestId = " + params[0], e);
        }

        if (prefResult.size() == 1) {
            Hdprofile hdprofile = (Hdprofile) prefResult.get(0);
            if (hdprofile.getValue("informfd").equalsIgnoreCase("1")) {
                //memberId = memberLoginInfo.getValue(DbConstants.LOGIN_ID);
                String fname = memberLoginInfo.getValue(DbConstants.FIRST_NAME);
                String lname = memberLoginInfo.getValue(DbConstants.LAST_NAME);
                String subject = "Invitation for friendship with " + fname + " " + lname;
                String msg = "Hi, " + gfname + "\n" + fname + " " + lname + " has sent you " + "\n\n"
                        + "Members webpage: " + webconstants.getHddomain() + "/userpage?member="
                        + memberLoginInfo.getValue(DbConstants.LOGIN) + webconstants.getMailfooter();
                hdmail.sendEmail(to, subject, msg, from);
            }
        }
    }

    Fqn fqn = cacheUtil.fqn(DbConstants.USER_PAGE);
    if (treeCache.exists(fqn, guestLogin)) {
        treeCache.remove(fqn, guestLogin);
    }

    fqn = cacheUtil.fqn(DbConstants.USER_PAGE);
    if (treeCache.exists(fqn, memberLoginInfo.getValue(DbConstants.LOGIN))) {
        treeCache.remove(fqn, memberLoginInfo.getValue(DbConstants.LOGIN));
    }
}

From source file:net.sf.taverna.t2.provenance.api.ProvenanceAccess.java

/**
 * Initialises a named JNDI DataSource if not already set up externally.
 * The DataSource is named jdbc/taverna//from  w w  w  . j av a 2 s. c  o  m
 *
 * @param driverClassName - the classname for the driver to be used.
 * @param jdbcUrl - the jdbc connection url
 * @param username - the username, if required (otherwise null)
 * @param password - the password, if required (oteherwise null)
 * @param minIdle - if the driver supports multiple connections, then the minumum number of idle connections in the pool
 * @param maxIdle - if the driver supports multiple connections, then the maximum number of idle connections in the pool
 * @param maxActive - if the driver supports multiple connections, then the minumum number of connections in the pool
 */
public static void initDataSource(String driverClassName, String jdbcUrl, String username, String password,
        int minIdle, int maxIdle, int maxActive) {
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.osjava.sj.memory.MemoryContextFactory");
    System.setProperty("org.osjava.sj.jndi.shared", "true");

    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(driverClassName);
    ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
    ds.setMaxActive(maxActive);
    ds.setMinIdle(minIdle);
    ds.setMaxIdle(maxIdle);
    ds.setDefaultAutoCommit(true);
    if (username != null) {
        ds.setUsername(username);
    }
    if (password != null) {
        ds.setPassword(password);
    }

    ds.setUrl(jdbcUrl);

    InitialContext context;
    try {
        context = new InitialContext();
        context.rebind("jdbc/taverna", ds);
    } catch (NamingException ex) {
        logger.error("Problem rebinding the jdbc context: " + ex);
    }

}