This commit is contained in:
Redkale
2018-05-02 08:28:18 +08:00
parent be2a4d252f
commit 5e5280a7fd
3 changed files with 27 additions and 14 deletions

View File

@@ -52,7 +52,7 @@ public class PoolJdbcSource extends PoolSource<Connection> {
try { try {
pc.close(); pc.close();
} catch (Exception e) { } catch (Exception e) {
dataSource.logger.log(Level.INFO, DataSource.class.getSimpleName() + " " + pc + " close error", e); logger.log(Level.INFO, DataSource.class.getSimpleName() + " " + pc + " close error", e);
} }
} }
} }
@@ -61,14 +61,14 @@ public class PoolJdbcSource extends PoolSource<Connection> {
public void connectionErrorOccurred(ConnectionEvent event) { public void connectionErrorOccurred(ConnectionEvent event) {
usingCounter.decrementAndGet(); usingCounter.decrementAndGet();
if ("08S01".equals(event.getSQLException().getSQLState())) return; //MySQL特性 长时间连接没使用会抛出com.mysql.jdbc.exceptions.jdbc4.CommunicationsException if ("08S01".equals(event.getSQLException().getSQLState())) return; //MySQL特性 长时间连接没使用会抛出com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
dataSource.logger.log(Level.WARNING, "connectionErronOccurred [" + event.getSQLException().getSQLState() + "]", event.getSQLException()); logger.log(Level.WARNING, "connectionErronOccurred [" + event.getSQLException().getSQLState() + "]", event.getSQLException());
} }
}; };
try { try {
this.watch(); this.watch();
} catch (Exception e) { } catch (Exception e) {
dataSource.logger.log(Level.WARNING, DataSource.class.getSimpleName() + " watch " + dataSource.conf + " error", e); logger.log(Level.WARNING, DataSource.class.getSimpleName() + " watch " + dataSource.conf + " error", e);
} }
} }
@@ -202,14 +202,14 @@ public class PoolJdbcSource extends PoolSource<Connection> {
if (property == null) property = m.get(pool.dataSource.name + "." + pool.rwtype); if (property == null) property = m.get(pool.dataSource.name + "." + pool.rwtype);
if (property != null) pool.change(property); if (property != null) pool.change(property);
} catch (Exception ex) { } catch (Exception ex) {
dataSource.logger.log(Level.INFO, event.context() + " occur error", ex); logger.log(Level.INFO, event.context() + " occur error", ex);
} }
} }
}); });
key.reset(); key.reset();
} }
} catch (Exception e) { } catch (Exception e) {
dataSource.logger.log(Level.WARNING, "DataSource watch " + file + " occur error", e); logger.log(Level.WARNING, "DataSource watch " + file + " occur error", e);
} }
} }
}; };
@@ -217,7 +217,7 @@ public class PoolJdbcSource extends PoolSource<Connection> {
watchThread.setName("DataSource-Watch-" + maps.size() + "-Thread"); watchThread.setName("DataSource-Watch-" + maps.size() + "-Thread");
watchThread.setDaemon(true); watchThread.setDaemon(true);
watchThread.start(); watchThread.start();
dataSource.logger.log(Level.INFO, watchThread.getName() + " start watching " + file); logger.log(Level.INFO, watchThread.getName() + " start watching " + file);
//----------------------------------------------------------- //-----------------------------------------------------------
list.add(new WeakReference<>(this)); list.add(new WeakReference<>(this));
maps.put(file, new AbstractMap.SimpleEntry<>(watcher, list)); maps.put(file, new AbstractMap.SimpleEntry<>(watcher, list));
@@ -244,9 +244,9 @@ public class PoolJdbcSource extends PoolSource<Connection> {
this.url = newurl; this.url = newurl;
this.username = newuser; this.username = newuser;
this.password = newpassword; this.password = newpassword;
dataSource.logger.log(Level.INFO, DataSource.class.getSimpleName() + "(" + dataSource.name + "." + rwtype + ") change (" + property + ")"); logger.log(Level.INFO, DataSource.class.getSimpleName() + "(" + dataSource.name + "." + rwtype + ") change (" + property + ")");
} catch (Exception e) { } catch (Exception e) {
dataSource.logger.log(Level.SEVERE, DataSource.class.getSimpleName() + " dynamic change JDBC (url userName password) error", e); logger.log(Level.SEVERE, DataSource.class.getSimpleName() + " dynamic change JDBC (url userName password) error", e);
} }
} }
@@ -272,7 +272,7 @@ public class PoolJdbcSource extends PoolSource<Connection> {
private Connection poll(final int count, SQLException e) { private Connection poll(final int count, SQLException e) {
if (count >= 3) { if (count >= 3) {
dataSource.logger.log(Level.WARNING, "create pooled connection error", e); logger.log(Level.WARNING, "create pooled connection error", e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
PooledConnection result = queue.poll(); PooledConnection result = queue.poll();
@@ -281,7 +281,7 @@ public class PoolJdbcSource extends PoolSource<Connection> {
try { try {
result = queue.poll(6, TimeUnit.SECONDS); result = queue.poll(6, TimeUnit.SECONDS);
} catch (Exception t) { } catch (Exception t) {
dataSource.logger.log(Level.WARNING, "take pooled connection error", t); logger.log(Level.WARNING, "take pooled connection error", t);
} }
} }
if (result == null) { if (result == null) {
@@ -301,13 +301,13 @@ public class PoolJdbcSource extends PoolSource<Connection> {
try { try {
conn = result.getConnection(); conn = result.getConnection();
if (!conn.isValid(1)) { if (!conn.isValid(1)) {
dataSource.logger.info("sql connection is not vaild"); logger.info("sql connection is not vaild");
usingCounter.decrementAndGet(); usingCounter.decrementAndGet();
return poll(0, null); return poll(0, null);
} }
} catch (SQLException ex) { } catch (SQLException ex) {
if (!"08S01".equals(ex.getSQLState())) {//MySQL特性 长时间连接没使用会抛出com.mysql.jdbc.exceptions.jdbc4.CommunicationsException if (!"08S01".equals(ex.getSQLState())) {//MySQL特性 长时间连接没使用会抛出com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
dataSource.logger.log(Level.FINER, "result.getConnection from pooled connection abort [" + ex.getSQLState() + "]", ex); logger.log(Level.FINER, "result.getConnection from pooled connection abort [" + ex.getSQLState() + "]", ex);
} }
return poll(0, null); return poll(0, null);
} }

View File

@@ -139,10 +139,10 @@ public abstract class PoolSource<T> {
public abstract CompletableFuture<T> pollAsync(); public abstract CompletableFuture<T> pollAsync();
public abstract void close();
public abstract void closeConnection(final T conn); public abstract void closeConnection(final T conn);
public abstract void close();
public final String getDbtype() { public final String getDbtype() {
return dbtype; return dbtype;
} }

View File

@@ -13,6 +13,7 @@ import java.util.Properties;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.logging.*; import java.util.logging.*;
import org.redkale.net.AsyncConnection; import org.redkale.net.AsyncConnection;
import static org.redkale.source.DataSources.*;
import org.redkale.util.ObjectPool; import org.redkale.util.ObjectPool;
/** /**
@@ -56,6 +57,18 @@ public abstract class PoolTcpSource extends PoolSource<AsyncConnection> {
} }
} }
@Override
public void change(Properties prop) {
String newurl = prop.getProperty(JDBC_URL);
String newuser = prop.getProperty(JDBC_USER, "");
String newpassword = prop.getProperty(JDBC_PWD, "");
if (this.url.equals(newurl) && this.username.equals(newuser) && this.password.equals(newpassword)) return;
this.url = newurl;
this.username = newuser;
this.password = newpassword;
parseAddressAndDbnameAndAttrs();
}
@Override @Override
public final AsyncConnection poll() { public final AsyncConnection poll() {
return pollAsync().join(); return pollAsync().join();