This commit is contained in:
Redkale
2016-10-22 10:23:53 +08:00
parent ecf102aff3
commit ffcd04a626
3 changed files with 23 additions and 7 deletions

View File

@@ -10,8 +10,9 @@
<!--
javax.persistence.jdbc.driver在JPA的值是JDBC驱动Redkale有所不同值应该是javax.sql.DataSource的子类。
为了兼容用户习惯Redkale内置常见JDBC驱动到javax.sql.DataSource的映射关系
com.mysql.jdbc.Driver —————— com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource
org.mariadb.jdbc.Driver —————— org.mariadb.jdbc.MySQLDataSource
org.postgresql.Driver —————— org.postgresql.ds.PGConnectionPoolDataSource
com.mysql.jdbc.Driver —————— com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource
oracle.jdbc.driver.OracleDriver —————— oracle.jdbc.pool.OracleConnectionPoolDataSource
com.microsoft.sqlserver.jdbc.SQLServerDriver —————— com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource
因此 com.mysql.jdbc.Driver 会被自动转换成 com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource

View File

@@ -204,6 +204,9 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
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;
@@ -211,13 +214,21 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
}
final Class clazz = Class.forName(source);
Object pdsource = clazz.newInstance();
Method seturlm;
try {
seturlm = clazz.getMethod("setUrl", String.class);
} catch (Exception e) {
seturlm = clazz.getMethod("setURL", String.class);
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);
}
seturlm.invoke(pdsource, url);
clazz.getMethod("setUser", String.class).invoke(pdsource, user);
clazz.getMethod("setPassword", String.class).invoke(pdsource, password);
return (ConnectionPoolDataSource) pdsource;

View File

@@ -109,6 +109,10 @@ public class JDBCPoolSource {
return source != null && source.getClass().getName().contains(".sqlserver.");
}
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();