This commit is contained in:
@@ -6,7 +6,6 @@
|
||||
package org.redkale.source;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
@@ -14,11 +13,11 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.*;
|
||||
import java.util.logging.*;
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.ConnectionPoolDataSource;
|
||||
import javax.xml.stream.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
* DataSource的JDBC实现类
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
@@ -30,26 +29,6 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
||||
|
||||
public static final String DATASOURCE_CONFPATH = "DATASOURCE_CONFPATH";
|
||||
|
||||
static final String JDBC_CONNECTIONSMAX = "javax.persistence.connections.limit";
|
||||
|
||||
static final String JDBC_CONTAIN_SQLTEMPLATE = "javax.persistence.contain.sqltemplate";
|
||||
|
||||
static final String JDBC_NOTCONTAIN_SQLTEMPLATE = "javax.persistence.notcontain.sqltemplate";
|
||||
|
||||
static final String JDBC_TABLENOTEXIST_SQLSTATES = "javax.persistence.tablenotexist.sqlstates";
|
||||
|
||||
static final String JDBC_TABLECOPY_SQLTEMPLATE = "javax.persistence.tablecopy.sqltemplate";
|
||||
|
||||
static final String JDBC_URL = "javax.persistence.jdbc.url";
|
||||
|
||||
static final String JDBC_USER = "javax.persistence.jdbc.user";
|
||||
|
||||
static final String JDBC_PWD = "javax.persistence.jdbc.password";
|
||||
|
||||
static final String JDBC_DRIVER = "javax.persistence.jdbc.driver";
|
||||
|
||||
static final String JDBC_SOURCE = "javax.persistence.jdbc.source";
|
||||
|
||||
private static final Flipper FLIPPER_ONE = new Flipper(1);
|
||||
|
||||
final Logger logger = Logger.getLogger(DataDefaultSource.class.getSimpleName());
|
||||
@@ -179,77 +158,6 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
|
||||
return map;
|
||||
}
|
||||
|
||||
static ConnectionPoolDataSource createDataSource(Properties property) {
|
||||
try {
|
||||
return createDataSource(property.getProperty(JDBC_SOURCE, property.getProperty(JDBC_DRIVER)),
|
||||
property.getProperty(JDBC_URL), property.getProperty(JDBC_USER), property.getProperty(JDBC_PWD));
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException("(" + property + ") have no jdbc parameters", ex);
|
||||
}
|
||||
}
|
||||
|
||||
static ConnectionPoolDataSource createDataSource(String source0, String url, String user, String password) throws Exception {
|
||||
String source = source0;
|
||||
if (source0 == null || source0.isEmpty()) {
|
||||
if (url.startsWith("jdbc:mysql:")) {
|
||||
source0 = "com.mysql.jdbc.Driver";
|
||||
} else if (url.startsWith("jdbc:mariadb:")) {
|
||||
source0 = "org.mariadb.jdbc.Driver";
|
||||
} else if (url.startsWith("jdbc:oracle:")) {
|
||||
source0 = "oracle.jdbc.driver.OracleDriver";
|
||||
} else if (url.startsWith("jdbc:postgresql:")) {
|
||||
source0 = "org.postgresql.Driver";
|
||||
} else if (url.startsWith("jdbc:microsoft:sqlserver:")) {
|
||||
source0 = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
|
||||
}
|
||||
}
|
||||
if (source0 != null && source0.contains("Driver")) { //为了兼容JPA的配置文件
|
||||
switch (source0) {
|
||||
case "org.mariadb.jdbc.Driver":
|
||||
source = "org.mariadb.jdbc.MySQLDataSource";
|
||||
break;
|
||||
case "com.mysql.cj.jdbc.Driver":
|
||||
case "com.mysql.jdbc.Driver":
|
||||
try {
|
||||
Class.forName("com.mysql.cj.jdbc.MysqlConnectionPoolDataSource");
|
||||
source = "com.mysql.cj.jdbc.MysqlConnectionPoolDataSource";
|
||||
} catch (Exception e) {
|
||||
source = "com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource";
|
||||
}
|
||||
break;
|
||||
case "oracle.jdbc.driver.OracleDriver":
|
||||
source = "oracle.jdbc.pool.OracleConnectionPoolDataSource";
|
||||
break;
|
||||
case "org.postgresql.Driver":
|
||||
source = "org.postgresql.ds.PGConnectionPoolDataSource";
|
||||
break;
|
||||
case "com.microsoft.sqlserver.jdbc.SQLServerDriver":
|
||||
source = "com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource";
|
||||
break;
|
||||
}
|
||||
}
|
||||
final Class clazz = Class.forName(source);
|
||||
Object pdsource = clazz.newInstance();
|
||||
if (source.contains(".postgresql.")) {
|
||||
Class driver = Class.forName("org.postgresql.Driver");
|
||||
Properties properties = (Properties) driver.getMethod("parseURL", String.class, Properties.class).invoke(null, url, new Properties());
|
||||
clazz.getMethod("setServerName", String.class).invoke(pdsource, properties.getProperty("PGHOST"));
|
||||
clazz.getMethod("setDatabaseName", String.class).invoke(pdsource, properties.getProperty("PGDBNAME"));
|
||||
clazz.getMethod("setPortNumber", int.class).invoke(pdsource, Integer.parseInt(properties.getProperty("PGPORT", "5432")));
|
||||
} else {
|
||||
Method seturlm;
|
||||
try {
|
||||
seturlm = clazz.getMethod("setUrl", String.class);
|
||||
} catch (Exception e) {
|
||||
seturlm = clazz.getMethod("setURL", String.class);
|
||||
}
|
||||
seturlm.invoke(pdsource, url);
|
||||
}
|
||||
clazz.getMethod("setUser", String.class).invoke(pdsource, user);
|
||||
clazz.getMethod("setPassword", String.class).invoke(pdsource, password);
|
||||
return (ConnectionPoolDataSource) pdsource;
|
||||
}
|
||||
|
||||
public final String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.*;
|
||||
import java.util.logging.Level;
|
||||
import javax.persistence.*;
|
||||
import static org.redkale.source.DataDefaultSource.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
@@ -278,11 +277,11 @@ public final class EntityInfo<T> {
|
||||
this.cache = null;
|
||||
}
|
||||
if (conf == null) conf = new Properties();
|
||||
this.containSQL = conf.getProperty(JDBC_CONTAIN_SQLTEMPLATE, "LOCATE(${keystr}, ${column}) > 0");
|
||||
this.notcontainSQL = conf.getProperty(JDBC_NOTCONTAIN_SQLTEMPLATE, "LOCATE(${keystr}, ${column}) = 0");
|
||||
this.containSQL = conf.getProperty(JDBCPoolSource.JDBC_CONTAIN_SQLTEMPLATE, "LOCATE(${keystr}, ${column}) > 0");
|
||||
this.notcontainSQL = conf.getProperty(JDBCPoolSource.JDBC_NOTCONTAIN_SQLTEMPLATE, "LOCATE(${keystr}, ${column}) = 0");
|
||||
|
||||
this.tablenotexistSqlstates = ";" + conf.getProperty(JDBC_TABLENOTEXIST_SQLSTATES, "42000;42S02") + ";";
|
||||
this.tablecopySQL = conf.getProperty(JDBC_TABLECOPY_SQLTEMPLATE, "CREATE TABLE ${newtable} LIKE ${oldtable}");
|
||||
this.tablenotexistSqlstates = ";" + conf.getProperty(JDBCPoolSource.JDBC_TABLENOTEXIST_SQLSTATES, "42000;42S02") + ";";
|
||||
this.tablecopySQL = conf.getProperty(JDBCPoolSource.JDBC_TABLECOPY_SQLTEMPLATE, "CREATE TABLE ${newtable} LIKE ${oldtable}");
|
||||
}
|
||||
|
||||
public void createPrimaryValue(T src) {
|
||||
|
||||
@@ -27,6 +27,26 @@ import javax.sql.*;
|
||||
*/
|
||||
public class JDBCPoolSource {
|
||||
|
||||
static final String JDBC_CONNECTIONSMAX = "javax.persistence.connections.limit";
|
||||
|
||||
static final String JDBC_CONTAIN_SQLTEMPLATE = "javax.persistence.contain.sqltemplate";
|
||||
|
||||
static final String JDBC_NOTCONTAIN_SQLTEMPLATE = "javax.persistence.notcontain.sqltemplate";
|
||||
|
||||
static final String JDBC_TABLENOTEXIST_SQLSTATES = "javax.persistence.tablenotexist.sqlstates";
|
||||
|
||||
static final String JDBC_TABLECOPY_SQLTEMPLATE = "javax.persistence.tablecopy.sqltemplate";
|
||||
|
||||
static final String JDBC_URL = "javax.persistence.jdbc.url";
|
||||
|
||||
static final String JDBC_USER = "javax.persistence.jdbc.user";
|
||||
|
||||
static final String JDBC_PWD = "javax.persistence.jdbc.password";
|
||||
|
||||
static final String JDBC_DRIVER = "javax.persistence.jdbc.driver";
|
||||
|
||||
static final String JDBC_SOURCE = "javax.persistence.jdbc.source";
|
||||
|
||||
private static final Map<String, AbstractMap.SimpleEntry<WatchService, List<WeakReference<JDBCPoolSource>>>> maps = new HashMap<>();
|
||||
|
||||
private final AtomicLong usingCounter = new AtomicLong();
|
||||
@@ -97,6 +117,77 @@ public class JDBCPoolSource {
|
||||
}
|
||||
}
|
||||
|
||||
static ConnectionPoolDataSource createDataSource(Properties property) {
|
||||
try {
|
||||
return createDataSource(property.getProperty(JDBC_SOURCE, property.getProperty(JDBC_DRIVER)),
|
||||
property.getProperty(JDBC_URL), property.getProperty(JDBC_USER), property.getProperty(JDBC_PWD));
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException("(" + property + ") have no jdbc parameters", ex);
|
||||
}
|
||||
}
|
||||
|
||||
static ConnectionPoolDataSource createDataSource(String source0, String url, String user, String password) throws Exception {
|
||||
String source = source0;
|
||||
if (source0 == null || source0.isEmpty()) {
|
||||
if (url.startsWith("jdbc:mysql:")) {
|
||||
source0 = "com.mysql.jdbc.Driver";
|
||||
} else if (url.startsWith("jdbc:mariadb:")) {
|
||||
source0 = "org.mariadb.jdbc.Driver";
|
||||
} else if (url.startsWith("jdbc:oracle:")) {
|
||||
source0 = "oracle.jdbc.driver.OracleDriver";
|
||||
} else if (url.startsWith("jdbc:postgresql:")) {
|
||||
source0 = "org.postgresql.Driver";
|
||||
} else if (url.startsWith("jdbc:microsoft:sqlserver:")) {
|
||||
source0 = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
|
||||
}
|
||||
}
|
||||
if (source0 != null && source0.contains("Driver")) { //为了兼容JPA的配置文件
|
||||
switch (source0) {
|
||||
case "org.mariadb.jdbc.Driver":
|
||||
source = "org.mariadb.jdbc.MySQLDataSource";
|
||||
break;
|
||||
case "com.mysql.cj.jdbc.Driver":
|
||||
case "com.mysql.jdbc.Driver":
|
||||
try {
|
||||
Class.forName("com.mysql.cj.jdbc.MysqlConnectionPoolDataSource");
|
||||
source = "com.mysql.cj.jdbc.MysqlConnectionPoolDataSource";
|
||||
} catch (Exception e) {
|
||||
source = "com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource";
|
||||
}
|
||||
break;
|
||||
case "oracle.jdbc.driver.OracleDriver":
|
||||
source = "oracle.jdbc.pool.OracleConnectionPoolDataSource";
|
||||
break;
|
||||
case "org.postgresql.Driver":
|
||||
source = "org.postgresql.ds.PGConnectionPoolDataSource";
|
||||
break;
|
||||
case "com.microsoft.sqlserver.jdbc.SQLServerDriver":
|
||||
source = "com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource";
|
||||
break;
|
||||
}
|
||||
}
|
||||
final Class clazz = Class.forName(source);
|
||||
Object pdsource = clazz.newInstance();
|
||||
if (source.contains(".postgresql.")) {
|
||||
Class driver = Class.forName("org.postgresql.Driver");
|
||||
Properties properties = (Properties) driver.getMethod("parseURL", String.class, Properties.class).invoke(null, url, new Properties());
|
||||
clazz.getMethod("setServerName", String.class).invoke(pdsource, properties.getProperty("PGHOST"));
|
||||
clazz.getMethod("setDatabaseName", String.class).invoke(pdsource, properties.getProperty("PGDBNAME"));
|
||||
clazz.getMethod("setPortNumber", int.class).invoke(pdsource, Integer.parseInt(properties.getProperty("PGPORT", "5432")));
|
||||
} else {
|
||||
Method seturlm;
|
||||
try {
|
||||
seturlm = clazz.getMethod("setUrl", String.class);
|
||||
} catch (Exception e) {
|
||||
seturlm = clazz.getMethod("setURL", String.class);
|
||||
}
|
||||
seturlm.invoke(pdsource, url);
|
||||
}
|
||||
clazz.getMethod("setUser", String.class).invoke(pdsource, user);
|
||||
clazz.getMethod("setPassword", String.class).invoke(pdsource, password);
|
||||
return (ConnectionPoolDataSource) pdsource;
|
||||
}
|
||||
|
||||
final boolean isMysql() {
|
||||
return source != null && source.getClass().getName().contains(".mysql.");
|
||||
}
|
||||
@@ -109,10 +200,10 @@ public class JDBCPoolSource {
|
||||
return source != null && source.getClass().getName().contains(".sqlserver.");
|
||||
}
|
||||
|
||||
final boolean isPostgresql () {
|
||||
final boolean isPostgresql() {
|
||||
return source != null && source.getClass().getName().contains(".postgresql.");
|
||||
}
|
||||
|
||||
|
||||
private void watch() throws IOException {
|
||||
if (dataSource.conf == null || dataSource.name == null) return;
|
||||
final String file = dataSource.conf.getFile();
|
||||
|
||||
Reference in New Issue
Block a user