Fixed #18 Allow specifying a default transaction isolation level.

pull/22/head
Brett Wooldridge 11 years ago
parent 65045e2e64
commit 1df5f7bf10

@ -19,6 +19,8 @@ package com.zaxxer.hikari;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
@ -50,6 +52,7 @@ public final class HikariConfig implements HikariConfigMBean
// Properties NOT changeable at runtime
//
private int transactionIsolation;
private String poolName;
private String connectionTestQuery;
private String dataSourceClassName;
@ -79,6 +82,7 @@ public final class HikariConfig implements HikariConfigMBean
maxPoolSize = 60;
maxLifetime = MAX_LIFETIME;
poolName = "HikariPool-" + poolNumber++;
transactionIsolation = -1;
}
/**
@ -89,7 +93,7 @@ public final class HikariConfig implements HikariConfigMBean
public HikariConfig(Properties properties)
{
this();
PropertyBeanSetter.setTargetFromProperties(this, properties);
PropertyBeanSetter.setTargetFromProperties(this, properties);
}
@ -376,6 +380,32 @@ public final class HikariConfig implements HikariConfigMBean
this.shadedCodexMapping = mapping;
}
public int getTransactionIsolation()
{
return transactionIsolation;
}
/**
* Set the default transaction isolation level. The specified value is the
* constant name from the <code>Connection</code> class, eg.
* <code>TRANSACTION_REPEATABLE_READ</code>.
*
* @param isolationLevel the name of the isolation level
*/
public void setTransactionIsolation(String isolationLevel)
{
try
{
Field field = Connection.class.getField(isolationLevel);
int level = field.getInt(null);
this.transactionIsolation = level;
}
catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e)
{
throw new IllegalArgumentException("Invalid transaction isolation value: " + isolationLevel);
}
}
public void validate()
{
Logger logger = LoggerFactory.getLogger(getClass());

@ -58,6 +58,7 @@ public final class HikariPool implements HikariPoolMBean
private final boolean jdbc4ConnectionTest;
private final boolean isAutoCommit;
private final boolean delegationProxies;
private int transactionIsolation;
private final Timer houseKeepingTimer;
@ -79,6 +80,7 @@ public final class HikariPool implements HikariPoolMBean
this.jdbc4ConnectionTest = configuration.isJdbc4ConnectionTest();
this.leakDetectionThreshold = configuration.getLeakDetectionThreshold();
this.isAutoCommit = configuration.isAutoCommit();
this.transactionIsolation = configuration.getTransactionIsolation();
String dsClassName = configuration.getDataSourceClassName();
try
@ -162,6 +164,7 @@ public final class HikariPool implements HikariPoolMBean
}
connection.setAutoCommit(isAutoCommit);
connection.setTransactionIsolation(transactionIsolation);
connection.clearWarnings();
return connection;
@ -345,6 +348,11 @@ public final class HikariPool implements HikariPoolMBean
throw new RuntimeException("Connection not alive, retry.");
}
if (transactionIsolation < 0)
{
transactionIsolation = connection.getTransactionIsolation();
}
String initSql = configuration.getConnectionInitSql();
if (initSql != null && initSql.length() > 0)
{

@ -193,4 +193,14 @@ public class CreationTest
Assert.assertSame("Totals connections not as expected", 2, ds.pool.getTotalConnections());
Assert.assertSame("Idle connections not as expected", 2, ds.pool.getIdleConnections());
}
@Test
public void testIsolation() throws Exception
{
HikariConfig config = new HikariConfig();
config.setTransactionIsolation("TRANSACTION_REPEATABLE_READ");
int transactionIsolation = config.getTransactionIsolation();
Assert.assertSame(Connection.TRANSACTION_REPEATABLE_READ, transactionIsolation);
}
}

Loading…
Cancel
Save