Add auto commit setting, and population of driver properties.

pull/1/head
Brett Wooldridge 11 years ago
parent ec9fe36ffa
commit a6b27ef2a3

@ -48,6 +48,7 @@ public class HikariConfig implements HikariConfigMBean
private String connectionUrl;
private String dataSourceClassName;
private boolean isJdbc4connectionTest;
private boolean isAutoCommit;
private Properties driverProperties;
@ -63,6 +64,7 @@ public class HikariConfig implements HikariConfigMBean
acquireRetryDelay = 750;
connectionTimeout = 5000;
idleTimeout = TimeUnit.MINUTES.toMillis(10);
isAutoCommit = true;
isJdbc4connectionTest = true;
minPoolSize = 10;
maxPoolSize = 60;
@ -97,12 +99,12 @@ public class HikariConfig implements HikariConfigMBean
}
}
public void addDriverProperty(String propertyName, String value)
public void addDataSourceProperty(String propertyName, String value)
{
driverProperties.put(propertyName, value);
}
public Properties getDriverProperties()
public Properties getDataSourceProperties()
{
return driverProperties;
}
@ -217,6 +219,16 @@ public class HikariConfig implements HikariConfigMBean
this.idleTimeout = idleTimeoutMs;
}
public boolean isAutoCommit()
{
return isAutoCommit;
}
public void setAutoCommit(boolean isAutoCommit)
{
this.isAutoCommit = isAutoCommit;
}
public boolean isJdbc4ConnectionTest()
{
return isJdbc4connectionTest;

@ -37,8 +37,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zaxxer.hikari.proxy.IHikariConnectionProxy;
import com.zaxxer.hikari.proxy.ProxyFactory;
import com.zaxxer.hikari.proxy.JavassistProxyFactoryFactory;
import com.zaxxer.hikari.util.ClassLoaderUtils;
import com.zaxxer.hikari.util.PropertyBeanSetter;
public class HikariPool implements HikariPoolMBean
{
@ -77,6 +78,7 @@ public class HikariPool implements HikariPoolMBean
{
Class<?> clazz = ClassLoaderUtils.loadClass(configuration.getDataSourceClassName());
this.dataSource = (DataSource) clazz.newInstance();
PropertyBeanSetter.setTargetFromProperties(dataSource, configuration.getDataSourceProperties());
}
catch (Exception e)
{
@ -141,6 +143,7 @@ public class HikariPool implements HikariPoolMBean
connectionProxy.captureStack(configuration.getLeakDetectionThreshold(), houseKeepingTimer);
}
connectionProxy.unclose();
inUseConnections.add(connectionProxy);
return connection;
@ -242,11 +245,12 @@ public class HikariPool implements HikariPoolMBean
try
{
Connection connection = dataSource.getConnection();
IHikariConnectionProxy proxyConnection = (IHikariConnectionProxy) ProxyFactory.INSTANCE.getProxyConnection(this, connection);
IHikariConnectionProxy proxyConnection = (IHikariConnectionProxy) JavassistProxyFactoryFactory.getProxyFactory().getProxyConnection(this, connection);
boolean alive = isConnectionAlive((Connection) proxyConnection, configuration.getConnectionTimeout());
if (alive)
{
connection.setAutoCommit(configuration.isAutoCommit());
idleConnectionCount.incrementAndGet();
totalConnections.incrementAndGet();
idleConnections.add(proxyConnection);

@ -16,11 +16,53 @@
package com.zaxxer.hikari.util;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Brett Wooldridge
*/
public class PropertyBeanSetter
public final class PropertyBeanSetter
{
private static final Logger LOGGER = LoggerFactory.getLogger(PropertyBeanSetter.class);
public static void setTargetFromProperties(Object target, Properties properties)
{
if (target == null || properties == null)
{
return;
}
for (Object propKey : properties.keySet())
{
String propName = propKey.toString();
String capitalized = "set" + propName.substring(0, 1).toUpperCase() + propName.substring(1);
try
{
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(propName, target.getClass(), null, capitalized);
Method writeMethod = propertyDescriptor.getWriteMethod();
if (writeMethod == null)
{
LOGGER.error("Property setter {}() is does not exist on target class {}", capitalized, target.getClass());
continue;
}
writeMethod.invoke(target, properties.get(propKey));
}
catch (IntrospectionException e)
{
LOGGER.error("Property {} is does not exist on target class {}", propName, target.getClass());
}
catch (Exception e)
{
LOGGER.error("Exception setting property {} on target class {}", propName, target.getClass(), e);
}
}
}
}

Loading…
Cancel
Save