
org.zodiac.ds.api.DynamicDataSourceProxy Maven / Gradle / Ivy
package org.zodiac.ds.api;
import javax.sql.DataSource;
import org.zodiac.sdk.toolkit.util.ExceptionUtil;
import java.sql.Connection;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Dynamic data source proxy. Proxy a data source as a dynamic data source.
*
*/
public class DynamicDataSourceProxy implements DynamicDataSource {
private String id;
private volatile DatabaseType databaseType;
private DataSource proxy;
private Lock lock = new ReentrantLock();
public DynamicDataSourceProxy(String id, DatabaseType databaseType, DataSource proxy) {
this.id = id;
this.databaseType = databaseType;
this.proxy = proxy;
}
public DynamicDataSourceProxy(String id, DataSource proxy) {
this.id = id;
this.proxy = proxy;
}
@Override
public DataSource getNative() {
return proxy;
}
@Override
public String getId() {
return id;
}
@Override
public DatabaseType getType() {
if (databaseType == null) {
try {
lock.lock();
try {
if (databaseType != null) {
return databaseType;
}
try (Connection connection = proxy.getConnection()) {
databaseType = DatabaseType.fromJdbcUrl(connection.getMetaData().getURL());
}
} finally {
lock.unlock();
}
} catch (Exception e) {
ExceptionUtil.chuck(e);
}
}
return databaseType;
}
public DynamicDataSourceProxy setDatabaseType(DatabaseType databaseType) {
this.databaseType = databaseType;
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy