Merge pull request #550 from ash2k/improvements2

Improvements2
pull/566/head
Brett Wooldridge 9 years ago
commit 65e23b102a

@ -0,0 +1,17 @@
# EditorConfig is awesome: http://EditorConfig.org
# top-most EditorConfig file
root = true
[**]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
trim_trailing_whitespace = true
[**.{java,xml}]
indent_size = 3
[**.{yaml,yml}]
indent_size = 2

@ -110,15 +110,17 @@ public class HikariDataSource extends HikariConfig implements DataSource, Closea
@Override
public PrintWriter getLogWriter() throws SQLException
{
return (pool != null ? pool.getUnwrappedDataSource().getLogWriter() : null);
HikariPool p = pool;
return (p != null ? p.getUnwrappedDataSource().getLogWriter() : null);
}
/** {@inheritDoc} */
@Override
public void setLogWriter(PrintWriter out) throws SQLException
{
if (pool != null) {
pool.getUnwrappedDataSource().setLogWriter(out);
HikariPool p = pool;
if (p != null) {
p.getUnwrappedDataSource().setLogWriter(out);
}
}
@ -126,8 +128,9 @@ public class HikariDataSource extends HikariConfig implements DataSource, Closea
@Override
public void setLoginTimeout(int seconds) throws SQLException
{
if (pool != null) {
pool.getUnwrappedDataSource().setLoginTimeout(seconds);
HikariPool p = pool;
if (p != null) {
p.getUnwrappedDataSource().setLoginTimeout(seconds);
}
}
@ -135,7 +138,8 @@ public class HikariDataSource extends HikariConfig implements DataSource, Closea
@Override
public int getLoginTimeout() throws SQLException
{
return (pool != null ? pool.getUnwrappedDataSource().getLoginTimeout() : 0);
HikariPool p = pool;
return (p != null ? p.getUnwrappedDataSource().getLoginTimeout() : 0);
}
/** {@inheritDoc} */
@ -154,13 +158,15 @@ public class HikariDataSource extends HikariConfig implements DataSource, Closea
return (T) this;
}
if (pool != null) {
if (iface.isInstance(pool.getUnwrappedDataSource())) {
return (T) pool.getUnwrappedDataSource();
HikariPool p = pool;
if (p != null) {
final DataSource unwrappedDataSource = p.getUnwrappedDataSource();
if (iface.isInstance(unwrappedDataSource)) {
return (T) unwrappedDataSource;
}
if (pool.getUnwrappedDataSource() != null) {
return (T) pool.getUnwrappedDataSource().unwrap(iface);
if (unwrappedDataSource != null) {
return (T) unwrappedDataSource.unwrap(iface);
}
}
@ -174,14 +180,16 @@ public class HikariDataSource extends HikariConfig implements DataSource, Closea
if (iface.isInstance(this)) {
return true;
}
if (pool != null) {
if (iface.isInstance(pool.getUnwrappedDataSource())) {
HikariPool p = pool;
if (p != null) {
final DataSource unwrappedDataSource = p.getUnwrappedDataSource();
if (iface.isInstance(unwrappedDataSource)) {
return true;
}
if (pool.getUnwrappedDataSource() != null) {
return pool.getUnwrappedDataSource().isWrapperFor(iface);
if (unwrappedDataSource != null) {
return unwrappedDataSource.isWrapperFor(iface);
}
}
@ -195,12 +203,13 @@ public class HikariDataSource extends HikariConfig implements DataSource, Closea
boolean isAlreadySet = getMetricRegistry() != null;
super.setMetricRegistry(metricRegistry);
if (pool != null) {
HikariPool p = pool;
if (p != null) {
if (isAlreadySet) {
throw new IllegalStateException("MetricRegistry can only be set one time");
}
else {
pool.setMetricRegistry(super.getMetricRegistry());
p.setMetricRegistry(super.getMetricRegistry());
}
}
}
@ -212,12 +221,13 @@ public class HikariDataSource extends HikariConfig implements DataSource, Closea
boolean isAlreadySet = getMetricsTrackerFactory() != null;
super.setMetricsTrackerFactory(metricsTrackerFactory);
if (pool != null) {
HikariPool p = pool;
if (p != null) {
if (isAlreadySet) {
throw new IllegalStateException("MetricsTrackerFactory can only be set one time");
}
else {
pool.setMetricsTrackerFactory(super.getMetricsTrackerFactory());
p.setMetricsTrackerFactory(super.getMetricsTrackerFactory());
}
}
}
@ -229,12 +239,13 @@ public class HikariDataSource extends HikariConfig implements DataSource, Closea
boolean isAlreadySet = getHealthCheckRegistry() != null;
super.setHealthCheckRegistry(healthCheckRegistry);
if (pool != null) {
HikariPool p = pool;
if (p != null) {
if (isAlreadySet) {
throw new IllegalStateException("HealthCheckRegistry can only be set one time");
}
else {
pool.setHealthCheckRegistry(super.getHealthCheckRegistry());
p.setHealthCheckRegistry(super.getHealthCheckRegistry());
}
}
}
@ -246,8 +257,9 @@ public class HikariDataSource extends HikariConfig implements DataSource, Closea
*/
public void evictConnection(Connection connection)
{
if (!isClosed() && pool != null && connection.getClass().getName().startsWith("com.zaxxer.hikari")) {
pool.evictConnection(connection);
HikariPool p;
if (!isClosed() && (p = pool) != null && connection.getClass().getName().startsWith("com.zaxxer.hikari")) {
p.evictConnection(connection);
}
}
@ -257,8 +269,9 @@ public class HikariDataSource extends HikariConfig implements DataSource, Closea
*/
public void suspendPool()
{
if (!isClosed() && pool != null) {
pool.suspendPool();
HikariPool p;
if (!isClosed() && (p = pool) != null) {
p.suspendPool();
}
}
@ -267,8 +280,9 @@ public class HikariDataSource extends HikariConfig implements DataSource, Closea
*/
public void resumePool()
{
if (!isClosed() && pool != null) {
pool.resumePool();
HikariPool p;
if (!isClosed() && (p = pool) != null) {
p.resumePool();
}
}
@ -282,12 +296,14 @@ public class HikariDataSource extends HikariConfig implements DataSource, Closea
return;
}
if (pool != null) {
HikariPool p = pool;
if (p != null) {
try {
pool.shutdown();
p.shutdown();
}
catch (InterruptedException e) {
LOGGER.warn("Interrupted during closing", e);
LOGGER.warn("Interrupted during closing", e);
Thread.currentThread().interrupt();
}
}
}

@ -168,7 +168,8 @@ final class PoolEntry implements IConcurrentBagEntry
Connection close()
{
if (endOfLife != null && !endOfLife.isDone() && !endOfLife.cancel(false)) {
ScheduledFuture<?> eol = endOfLife;
if (eol != null && !eol.isDone() && !eol.cancel(false)) {
LOGGER.warn("{} - maxLifeTime expiration task cancellation unexpectedly returned false for connection {}", getPoolName(), connection);
}

@ -53,7 +53,6 @@ import static com.zaxxer.hikari.util.ConcurrentBag.IConcurrentBagEntry.STATE_RES
*
* @param <T> the templated type to store in the bag
*/
@SuppressWarnings("rawtypes")
public class ConcurrentBag<T extends IConcurrentBagEntry> implements AutoCloseable
{
private static final Logger LOGGER = LoggerFactory.getLogger(ConcurrentBag.class);
@ -62,7 +61,7 @@ public class ConcurrentBag<T extends IConcurrentBagEntry> implements AutoCloseab
private final CopyOnWriteArrayList<T> sharedList;
private final boolean weakThreadLocals;
private final ThreadLocal<List> threadList;
private final ThreadLocal<List<Object>> threadList;
private final IBagStateListener listener;
private final AtomicInteger waiters;
private volatile boolean closed;
@ -101,9 +100,9 @@ public class ConcurrentBag<T extends IConcurrentBagEntry> implements AutoCloseab
this.threadList = new ThreadLocal<>();
}
else {
this.threadList = new ThreadLocal<List>() {
this.threadList = new ThreadLocal<List<Object>>() {
@Override
protected List initialValue()
protected List<Object> initialValue()
{
return new FastList<>(IConcurrentBagEntry.class, 16);
}
@ -123,15 +122,16 @@ public class ConcurrentBag<T extends IConcurrentBagEntry> implements AutoCloseab
public T borrow(long timeout, final TimeUnit timeUnit) throws InterruptedException
{
// Try the thread-local list first
List<?> list = threadList.get();
List<Object> list = threadList.get();
if (weakThreadLocals && list == null) {
list = new ArrayList<>(16);
threadList.set(list);
}
for (int i = list.size() - 1; i >= 0; i--) {
final Object entry = list.remove(i);
@SuppressWarnings("unchecked")
final T bagEntry = (T) (weakThreadLocals ? ((WeakReference) list.remove(i)).get() : list.remove(i));
final T bagEntry = weakThreadLocals ? ((WeakReference<T>) entry).get() : (T) entry;
if (bagEntry != null && bagEntry.compareAndSet(STATE_NOT_IN_USE, STATE_IN_USE)) {
return bagEntry;
}
@ -184,14 +184,13 @@ public class ConcurrentBag<T extends IConcurrentBagEntry> implements AutoCloseab
* @throws NullPointerException if value is null
* @throws IllegalStateException if the requited value was not borrowed from the bag
*/
@SuppressWarnings("unchecked")
public void requite(final T bagEntry)
{
bagEntry.lazySet(STATE_NOT_IN_USE);
final List threadLocalList = threadList.get();
final List<Object> threadLocalList = threadList.get();
if (threadLocalList != null) {
threadLocalList.add((weakThreadLocals ? new WeakReference<>(bagEntry) : bagEntry));
threadLocalList.add(weakThreadLocals ? new WeakReference<>(bagEntry) : bagEntry);
}
synchronizer.signal();

Loading…
Cancel
Save