下面列出了java.sql.Connection#TRANSACTION_READ_COMMITTED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void assertIsoLocks(Connection conn, int expectedIsoLevel)
throws SQLException {
int conniso = conn.getTransactionIsolation();
assertEquals(expectedIsoLevel, conniso);
boolean selectTimedOut = selectTimesoutDuringUpdate(conn);
// expect a lock timeout for READ_COMMITTED
switch (conniso) {
case Connection.TRANSACTION_READ_UNCOMMITTED:
assertFalse(selectTimedOut); break;
case Connection.TRANSACTION_READ_COMMITTED:
assertTrue(selectTimedOut); break;
default:
System.out.println("No test support for isolation level");
}
}
public static byte getThriftTransactionIsolation(int jdbcIsolationLevel) {
switch (jdbcIsolationLevel) {
case Connection.TRANSACTION_NONE:
return gfxdConstants.TRANSACTION_NONE;
case Connection.TRANSACTION_READ_UNCOMMITTED:
return gfxdConstants.TRANSACTION_READ_UNCOMMITTED;
case Connection.TRANSACTION_READ_COMMITTED:
return gfxdConstants.TRANSACTION_READ_COMMITTED;
case Connection.TRANSACTION_REPEATABLE_READ:
return gfxdConstants.TRANSACTION_REPEATABLE_READ;
case Connection.TRANSACTION_SERIALIZABLE:
return gfxdConstants.TRANSACTION_SERIALIZABLE;
default:
return gfxdConstants.TRANSACTION_NO_CHANGE;
}
}
private static int getTxIsolation(Long key, String val) {
if (val.equalsIgnoreCase("none")) {
return TRANSACTION_NONE;
}
else {
if (val.equalsIgnoreCase("read_uncommitted") ||
val.equalsIgnoreCase("readUncommitted")) {
return Connection.TRANSACTION_READ_UNCOMMITTED;
}
else if (val.equalsIgnoreCase("read_committed") ||
val.equalsIgnoreCase("readCommitted")) {
return Connection.TRANSACTION_READ_COMMITTED;
}
else if (val.equalsIgnoreCase("repeatable_read") ||
val.equalsIgnoreCase("repeatableRead")) {
return Connection.TRANSACTION_REPEATABLE_READ;
}
else if (val.equalsIgnoreCase("serializable")) {
return Connection.TRANSACTION_SERIALIZABLE;
}
else {
String s = "Illegal value for " + BasePrms.nameForKey(key) + ": " + val;
throw new HydraConfigException(s);
}
}
}
@Override
public boolean supportsTransactionIsolationLevel(int level)
throws SQLException
{
logger.debug(
"public boolean supportsTransactionIsolationLevel(int level)");
raiseSQLExceptionIfConnectionIsClosed();
return (level == Connection.TRANSACTION_NONE)
|| (level == Connection.TRANSACTION_READ_COMMITTED);
}
/**
* @return
* @throws SQLException
* @throws UserStoreException
*/
protected Connection getDBConnection() throws SQLException, UserStoreException {
Connection dbConnection = getJDBCDataSource().getConnection();
dbConnection.setAutoCommit(false);
if (dbConnection.getTransactionIsolation() != Connection.TRANSACTION_READ_COMMITTED) {
dbConnection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
}
return dbConnection;
}
private static int getTxIsolation(Long key, String val) {
if (val.equalsIgnoreCase("transaction_none") ||
val.equalsIgnoreCase("transactionNone") ||
val.equalsIgnoreCase("none")) {
return TRANSACTION_NONE;
}
else {
if (val.equalsIgnoreCase("transaction_read_uncommitted") ||
val.equalsIgnoreCase("transactionReadUncommitted") ||
val.equalsIgnoreCase("read_uncommitted") ||
val.equalsIgnoreCase("readUncommitted")) {
return Connection.TRANSACTION_READ_UNCOMMITTED;
}
else if (val.equalsIgnoreCase("transaction_read_committed") ||
val.equalsIgnoreCase("transactionReadCommitted") ||
val.equalsIgnoreCase("read_committed") ||
val.equalsIgnoreCase("readCommitted")) {
return Connection.TRANSACTION_READ_COMMITTED;
}
else if (val.equalsIgnoreCase("transaction_repeatable_read") ||
val.equalsIgnoreCase("transactionRepeatableRead") ||
val.equalsIgnoreCase("repeatable_read") ||
val.equalsIgnoreCase("repeatableRead")) {
return Connection.TRANSACTION_REPEATABLE_READ;
}
else if (val.equalsIgnoreCase("transaction_serializable") ||
val.equalsIgnoreCase("transactionSerializable") ||
val.equalsIgnoreCase("serializable")) {
return Connection.TRANSACTION_SERIALIZABLE;
}
else {
String s = "Illegal value for " + BasePrms.nameForKey(key) + ": " + val;
throw new HydraConfigException(s);
}
}
}
@DataProvider(name = "rowSetIsolationTypes")
protected Object[][] rowSetIsolationTypes() throws Exception {
RowSet rs = newInstance();
return new Object[][]{
{rs, Connection.TRANSACTION_NONE},
{rs, Connection.TRANSACTION_READ_COMMITTED},
{rs, Connection.TRANSACTION_READ_UNCOMMITTED},
{rs, Connection.TRANSACTION_REPEATABLE_READ},
{rs, Connection.TRANSACTION_SERIALIZABLE}
};
}
private String getIsolation(int i) {
if (i == Connection.TRANSACTION_READ_COMMITTED) {
return "READ_COMMITTED";
}
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";
}
/**
* A connection to the database. Should be short-lived. No transaction active by default.
*
* @return a new open connection.
*/
public DbConn getConn()
{
Connection cnx = null;
try
{
Thread.interrupted(); // this is VERY sad. Needed for Oracle driver which otherwise fails spectacularly.
cnx = _ds.getConnection();
if (cnx.getAutoCommit())
{
cnx.setAutoCommit(false);
cnx.rollback(); // To ensure no open transaction created by the pool before changing TX mode
}
if (cnx.getTransactionIsolation() != Connection.TRANSACTION_READ_COMMITTED)
{
cnx.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
}
return new DbConn(this, cnx);
}
catch (SQLException e)
{
DbHelper.closeQuietly(cnx); // May have been left open when the pool has given us a failed connection.
throw new DatabaseException(e);
}
}
private static int getTransactionIsolation(String actionValue) {
if (actionValue.equals(HttpParameter.READ_UNCOMMITTED)) {
return Connection.TRANSACTION_READ_UNCOMMITTED;
} else if (actionValue.equals(HttpParameter.READ_COMMITTED)) {
return Connection.TRANSACTION_READ_COMMITTED;
} else if (actionValue.equals(HttpParameter.REPEATABLE_READ)) {
return Connection.TRANSACTION_REPEATABLE_READ;
} else if (actionValue.equals(HttpParameter.SERIALIZABLE)) {
return Connection.TRANSACTION_SERIALIZABLE;
} else {
throw new IllegalArgumentException(
"Unsupported Transaction Isolation Level: " + actionValue);
}
}
/**
* Parse the initial PBSD - PiggyBackedSessionData code point.
* <p>
* If sent by the server, it contains a PBSD_ISO code point followed by a
* byte representing the JDBC isolation level, and a PBSD_SCHEMA code point
* followed by the name of the current schema as an UTF-8 String.
*/
private void parseInitialPBSD() {
if (peekCodePoint() != CodePoint.PBSD) {
return;
}
parseLengthAndMatchCodePoint(CodePoint.PBSD);
int peekCP = peekCodePoint();
while (peekCP != END_OF_SAME_ID_CHAIN) {
parseLengthAndMatchCodePoint(peekCP);
switch (peekCP) {
case CodePoint.PBSD_ISO:
int isolationLevel = readUnsignedByte();
if (isolationLevel != Connection.TRANSACTION_READ_COMMITTED)
throw new IllegalStateException("Database using unsupported transaction isolation level: " + isolationLevel);
// netAgent_.netConnection_.
// completeInitialPiggyBackIsolation(readUnsignedByte());
break;
case CodePoint.PBSD_SCHEMA:
String pbSchema = readString(getDdmLength(), CCSIDConstants.UTF8);
// netAgent_.netConnection_.
// completeInitialPiggyBackSchema
// (readString(getDdmLength(), Typdef.UTF8ENCODING));
break;
default:
throw new IllegalStateException("Found unknown codepoint: " + Integer.toHexString(peekCP));
}
peekCP = peekCodePoint();
}
}
@DataProvider(name = "rowSetIsolationTypes")
protected Object[][] rowSetIsolationTypes() throws Exception {
RowSet rs = newInstance();
return new Object[][]{
{rs, Connection.TRANSACTION_NONE},
{rs, Connection.TRANSACTION_READ_COMMITTED},
{rs, Connection.TRANSACTION_READ_UNCOMMITTED},
{rs, Connection.TRANSACTION_REPEATABLE_READ},
{rs, Connection.TRANSACTION_SERIALIZABLE}
};
}
/**
* Create a RuntimeStatistics object to parse the text and extract
* information.
*
* @param rts
* Runtime Statistics string
*
*/
public RuntimeStatisticsParser(String rts) {
statistics = rts;
if (rts.indexOf(" at serializable isolation level ") != -1)
isolationLevel = Connection.TRANSACTION_SERIALIZABLE;
else if (rts.indexOf("at read uncommitted isolation level") != -1)
isolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED;
else if (rts.indexOf("at read committed isolation level") != -1)
isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
else if (rts.indexOf("at repeatable read isolation level") != -1)
isolationLevel = Connection.TRANSACTION_REPEATABLE_READ;
if (rts.indexOf("Distinct Scan ResultSet") > 0) {
distinctScan = true;
}
if (rts.indexOf("Table Scan ResultSet") > 0) {
tableScan = true;
}
indexScan = (rts.indexOf("Index Scan ResultSet") >= 0);
indexRowToBaseRow =
(rts.indexOf("Index Row to Base Row ResultSet") >= 0);
if (rts.indexOf("Eliminate duplicates = true") > 0) {
eliminatedDuplicates = true;
}
if (rts.indexOf("Scroll Insensitive ResultSet:") > 0)
scrollInsensitive = true;
qualifiers = findQualifiers();
}
private String getIsolation(int i) {
if (i == Connection.TRANSACTION_READ_COMMITTED) {
return "READ_COMMITTED";
}
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";
}
private String getIsolation(int i) {
if (i == Connection.TRANSACTION_READ_COMMITTED) {
return "READ_COMMITTED";
}
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";
}
private static int getTxIsolation(Long key, String val) {
if (val.equalsIgnoreCase("transaction_none") ||
val.equalsIgnoreCase("transactionNone") ||
val.equalsIgnoreCase("none")) {
return TRANSACTION_NONE;
}
else {
if (val.equalsIgnoreCase("transaction_read_uncommitted") ||
val.equalsIgnoreCase("transactionReadUncommitted") ||
val.equalsIgnoreCase("read_uncommitted") ||
val.equalsIgnoreCase("readUncommitted")) {
return Connection.TRANSACTION_READ_UNCOMMITTED;
}
else if (val.equalsIgnoreCase("transaction_read_committed") ||
val.equalsIgnoreCase("transactionReadCommitted") ||
val.equalsIgnoreCase("read_committed") ||
val.equalsIgnoreCase("readCommitted")) {
return Connection.TRANSACTION_READ_COMMITTED;
}
else if (val.equalsIgnoreCase("transaction_repeatable_read") ||
val.equalsIgnoreCase("transactionRepeatableRead") ||
val.equalsIgnoreCase("repeatable_read") ||
val.equalsIgnoreCase("repeatableRead")) {
return Connection.TRANSACTION_REPEATABLE_READ;
}
else if (val.equalsIgnoreCase("transaction_serializable") ||
val.equalsIgnoreCase("transactionSerializable") ||
val.equalsIgnoreCase("serializable")) {
return Connection.TRANSACTION_SERIALIZABLE;
}
else {
String s = "Illegal value for " + BasePrms.nameForKey(key) + ": " + val;
throw new HydraConfigException(s);
}
}
}
public static String getTxIsolation(int n) {
switch (n) {
case Connection.TRANSACTION_READ_COMMITTED:
return "read_committed";
case Connection.TRANSACTION_SERIALIZABLE:
return "serializable";
default:
String s = "Unknown transaction isolation level: " + n;
throw new HydraConfigException(s);
}
}
protected int getIsolationLevel() {
return Connection.TRANSACTION_READ_COMMITTED;
}
@Override
public int getIsolationLevel() {
return Connection.TRANSACTION_READ_COMMITTED;
}
protected int getIsolationLevel() {
return Connection.TRANSACTION_READ_COMMITTED;
}