From 0ec329927a6cc6c8b1df30859f1e2ab28272d543 Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Sat, 14 Apr 2018 09:55:17 +0800 Subject: [PATCH] --- src/org/redkale/source/PoolJdbcSource.java | 56 +++++++--------------- src/org/redkale/source/PoolSource.java | 53 ++++++++++++++++++++ 2 files changed, 69 insertions(+), 40 deletions(-) diff --git a/src/org/redkale/source/PoolJdbcSource.java b/src/org/redkale/source/PoolJdbcSource.java index 22d3cbe22..056544f7c 100644 --- a/src/org/redkale/source/PoolJdbcSource.java +++ b/src/org/redkale/source/PoolJdbcSource.java @@ -13,7 +13,6 @@ import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; import java.sql.*; import java.util.*; import java.util.concurrent.*; -import java.util.concurrent.atomic.AtomicLong; import java.util.logging.Level; import javax.sql.*; import static org.redkale.source.DataSources.*; @@ -25,18 +24,10 @@ import static org.redkale.source.DataSources.*; * * @author zhangjx */ -public class PoolJdbcSource { +public class PoolJdbcSource extends PoolSource { private static final Map>>> maps = new HashMap<>(); - private final AtomicLong usingCounter = new AtomicLong(); - - private final AtomicLong creatCounter = new AtomicLong(); - - private final AtomicLong cycleCounter = new AtomicLong(); - - private final AtomicLong saveCounter = new AtomicLong(); - private final ConnectionPoolDataSource source; private final ArrayBlockingQueue queue; @@ -45,27 +36,10 @@ public class PoolJdbcSource { private final DataJdbcSource dataSource; - private final String stype; // "" 或 "read" 或 "write" - - private final int maxconns; - - private String url; - - private String user; - - private String password; - - final Properties props; - public PoolJdbcSource(DataJdbcSource source, String stype, Properties prop) { + super(stype, prop, source.logger); this.dataSource = source; - this.stype = stype; - this.props = prop; this.source = createDataSource(prop); - this.url = prop.getProperty(JDBC_URL); - this.user = prop.getProperty(JDBC_USER); - this.password = prop.getProperty(JDBC_PWD); - this.maxconns = Integer.decode(prop.getProperty(JDBC_CONNECTIONSMAX, "" + Runtime.getRuntime().availableProcessors() * 16)); this.queue = new ArrayBlockingQueue<>(this.maxconns); this.listener = new ConnectionEventListener() { @@ -251,6 +225,7 @@ public class PoolJdbcSource { } } + @Override public void change(Properties property) { Method seturlm; Class clazz = source.getClass(); @@ -276,10 +251,21 @@ public class PoolJdbcSource { } } + @Override + public boolean isAysnc() { + return false; + } + + @Override public Connection poll() { return poll(0, null); } + @Override + public CompletableFuture pollAsync() { + return CompletableFuture.completedFuture(poll()); + } + private Connection poll(final int count, SQLException e) { if (count >= 3) { dataSource.logger.log(Level.WARNING, "create pooled connection error", e); @@ -324,18 +310,7 @@ public class PoolJdbcSource { return conn; } - public long getCreatCount() { - return creatCounter.longValue(); - } - - public long getCycleCount() { - return cycleCounter.longValue(); - } - - public long getSaveCount() { - return saveCounter.longValue(); - } - + @Override public void close() { queue.stream().forEach(x -> { try { @@ -344,4 +319,5 @@ public class PoolJdbcSource { } }); } + } diff --git a/src/org/redkale/source/PoolSource.java b/src/org/redkale/source/PoolSource.java index 7034b9ea1..8272a984f 100644 --- a/src/org/redkale/source/PoolSource.java +++ b/src/org/redkale/source/PoolSource.java @@ -7,6 +7,9 @@ package org.redkale.source; import java.util.Properties; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicLong; +import java.util.logging.Logger; +import static org.redkale.source.DataSources.*; /** * 连接池类 @@ -19,6 +22,40 @@ import java.util.concurrent.CompletableFuture; */ public abstract class PoolSource { + protected final AtomicLong usingCounter = new AtomicLong(); + + protected final AtomicLong creatCounter = new AtomicLong(); + + protected final AtomicLong cycleCounter = new AtomicLong(); + + protected final AtomicLong saveCounter = new AtomicLong(); + + protected final Logger logger; + + protected final String stype; // "" 或 "read" 或 "write" + + protected final int maxconns; + + protected String url; + + protected String user; + + protected String password; + + protected String defdb; + + protected Properties props; + + public PoolSource(String stype, Properties prop, Logger logger) { + this.logger = logger; + this.stype = stype; + this.props = prop; + this.url = prop.getProperty(JDBC_URL); + this.user = prop.getProperty(JDBC_USER); + this.password = prop.getProperty(JDBC_PWD); + this.maxconns = Integer.decode(prop.getProperty(JDBC_CONNECTIONSMAX, "" + Runtime.getRuntime().availableProcessors() * 16)); + } + /** * 是否异步, 为true则只能调用pollAsync方法,为false则只能调用poll方法 * @@ -33,4 +70,20 @@ public abstract class PoolSource { public abstract CompletableFuture pollAsync(); public abstract void close(); + + public final long getUsingCount() { + return usingCounter.longValue(); + } + + public final long getCreatCount() { + return creatCounter.longValue(); + } + + public final long getCycleCount() { + return cycleCounter.longValue(); + } + + public final long getSaveCount() { + return saveCounter.longValue(); + } }