This commit is contained in:
@@ -102,7 +102,36 @@ public final class DataSources {
|
|||||||
if (readprop == null) throw new IOException("Cannot find (resource.name = '" + unitName + "') DataSource");
|
if (readprop == null) throw new IOException("Cannot find (resource.name = '" + unitName + "') DataSource");
|
||||||
if (writeprop == null) writeprop = readprop;
|
if (writeprop == null) writeprop = readprop;
|
||||||
String impl = readprop.getProperty(JDBC_DATASOURCE_CLASS, DataJdbcSource.class.getName());
|
String impl = readprop.getProperty(JDBC_DATASOURCE_CLASS, DataJdbcSource.class.getName());
|
||||||
if (DataJdbcSource.class.getName().equals(impl)) return new DataJdbcSource(unitName, persistxml, readprop, writeprop);
|
if (DataJdbcSource.class.getName().equals(impl)) {
|
||||||
|
try {
|
||||||
|
return new DataJdbcSource(unitName, persistxml, readprop, writeprop);
|
||||||
|
} catch (RuntimeException re) {
|
||||||
|
if (!(re.getCause() instanceof ClassNotFoundException)) throw re;
|
||||||
|
String dbtype = null;
|
||||||
|
{
|
||||||
|
/* jdbc:mysql:// jdbc:microsoft:sqlserver:// 取://之前的到最后一个:之间的字符串 */
|
||||||
|
String url = readprop.getProperty(JDBC_URL);
|
||||||
|
int pos = url.indexOf("://");
|
||||||
|
if (pos > 0) {
|
||||||
|
String url0 = url.substring(0, pos);
|
||||||
|
pos = url0.lastIndexOf(':');
|
||||||
|
if (pos > 0) dbtype = url0.substring(pos + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (dbtype == null) throw re;
|
||||||
|
Iterator<SourceLoader> it = ServiceLoader.load(SourceLoader.class).iterator();
|
||||||
|
Class dsClass = null;
|
||||||
|
while (it.hasNext()) {
|
||||||
|
SourceLoader loader = it.next();
|
||||||
|
if (dbtype.equalsIgnoreCase(loader.dbtype())) {
|
||||||
|
dsClass = loader.dataSourceClass();
|
||||||
|
if (dsClass != null) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (dsClass == null) throw re;
|
||||||
|
impl = dsClass.getName();
|
||||||
|
}
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
Class ds = Thread.currentThread().getContextClassLoader().loadClass(impl);
|
Class ds = Thread.currentThread().getContextClassLoader().loadClass(impl);
|
||||||
for (Constructor d : ds.getConstructors()) {
|
for (Constructor d : ds.getConstructors()) {
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ public class PoolJdbcSource extends PoolSource<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static ConnectionPoolDataSource createDataSource(Properties property) {
|
private static ConnectionPoolDataSource createDataSource(Properties property) {
|
||||||
try {
|
try {
|
||||||
return createDataSource(property.getProperty(JDBC_SOURCE, property.getProperty(JDBC_DRIVER)),
|
return createDataSource(property.getProperty(JDBC_SOURCE, property.getProperty(JDBC_DRIVER)),
|
||||||
property.getProperty(JDBC_URL), property.getProperty(JDBC_USER), property.getProperty(JDBC_PWD));
|
property.getProperty(JDBC_URL), property.getProperty(JDBC_USER), property.getProperty(JDBC_PWD));
|
||||||
@@ -85,7 +85,7 @@ public class PoolJdbcSource extends PoolSource<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static ConnectionPoolDataSource createDataSource(String source0, String url, String user, String password) throws Exception {
|
private static ConnectionPoolDataSource createDataSource(String source0, String url, String user, String password) throws Exception {
|
||||||
String source = source0;
|
String source = source0;
|
||||||
if (source0 == null || source0.isEmpty()) {
|
if (source0 == null || source0.isEmpty()) {
|
||||||
if (url.startsWith("jdbc:mysql:")) {
|
if (url.startsWith("jdbc:mysql:")) {
|
||||||
|
|||||||
22
src/org/redkale/source/SourceLoader.java
Normal file
22
src/org/redkale/source/SourceLoader.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package org.redkale.source;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义的DataSource加载器
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 详情见: https://redkale.org
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
*/
|
||||||
|
public interface SourceLoader {
|
||||||
|
|
||||||
|
public String dbtype();
|
||||||
|
|
||||||
|
public Class<? extends DataSource> dataSourceClass();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user