我之前多次遇到过这个问题,并使用 Spring 的 DelegatingDataSource 解决了它,它允许您定义多个 DataSource 对象并通过某种类型的查找键委托给所需的正确目标数据源。 TransactionAwareDataSourceProxy 这可能是您在帖子中显示的代码的一个不错选择的子类,正如 JavaDoc 描述的第一句话所述:
目标 JDBC 数据源的代理,增加 Spring 管理事务的意识。类似于 Java EE 服务器提供的事务性 JNDI 数据源。
我通常总是在任何给定线程中使用相同的目标数据源,所以我倾向于将我的查找键放入 ThreadLocal 对象中,并让代理数据源读取它以在调用 @ 时找到实际的目标数据源987654325@制作完成。
如果您创建这样一个委托(代理)数据源,您的EntityManagerFactory 可以将其用作其底层 JDBC 数据源,并在任何给定时间根据需要委托给正确的目标数据源。
多年来,我一直在 JPA 代码中使用这种类型的方法,我需要使用相同的持久性单元访问多个数据源,这对我来说非常有用。也应该与 Spring JPA 数据存储库一起正常工作。
以下是我之前参与的一个项目的一些实现代码。代码属于我,所以随意复制你喜欢的任何内容,并随心所欲地使用它。
这里是代理DataSource 类,它委托给实际目标 JDBC DataSource。它确实不扩展了 Spring 的 DelegatingDataSource,如上所述,但它做的事情完全相同。如果您不熟悉 OSGI 声明式服务及其注释(我想大多数人不熟悉),@Component(property = {"osgi.jndi.service.name=jdbc/customation"} 就是将DataSource 放入 JNDI 注册表的原因,以便它可以通过持久性单元描述符( persistence.xml) 显示在下面。
package com.custsoft.client.ds;
import com.custsoft.client.ClientXrefHolder;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import static com.custsoft.Constants.CLIENT;
/**
* Proxy data source that delegates to an actual JDBC data source.
* There is one target JDBC data source per client.
*
* Created by eric on 9/29/15.
*/
@Component(property = {"osgi.jndi.service.name=jdbc/customation"},
service = DataSource.class)
public class ClientDelegatingDataSource implements DataSource {
private static final Logger logger = LoggerFactory.getLogger(ClientDelegatingDataSource.class);
private String DEFAULT_CLIENT_XREF = "customation";
private Map<String, DataSource> clientDataSources = new HashMap<>();
@Reference(target = "(client=*)",
cardinality = ReferenceCardinality.MULTIPLE,
policy = ReferencePolicy.DYNAMIC)
protected void addDataSource(DataSource dataSource, Map<String, Object> properties) {
final String clientId = getClientId(properties);
clientDataSources.put(clientId, dataSource);
}
protected void removeDataSource(DataSource dataSource, Map<String, Object> properties) {
final String clientId = getClientId(properties);
clientDataSources.remove(clientId);
}
private String getClientId(Map<String, Object> properties) {
return Objects.toString(properties.get(CLIENT), null);
}
@Override
public Connection getConnection() throws SQLException {
return determineTargetDataSource().getConnection();
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
return determineTargetDataSource().getConnection(username, password);
}
private DataSource determineTargetDataSource() {
String clientId = ClientXrefHolder.getClientXref();
if (clientId == null) {
clientId = DEFAULT_CLIENT_XREF;
}
DataSource dataSource = clientDataSources.get(clientId);
if (dataSource == null) {
final String message = String.format(
"Couldn't find data source for client \"%s\".", clientId);
throw new IllegalStateException(message);
}
return dataSource;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return determineTargetDataSource().unwrap(iface);
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return determineTargetDataSource().isWrapperFor(iface);
}
@Override
public PrintWriter getLogWriter() throws SQLException {
return determineTargetDataSource().getLogWriter();
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
determineTargetDataSource().setLogWriter(out);
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
determineTargetDataSource().setLoginTimeout(seconds);
}
@Override
public int getLoginTimeout() throws SQLException {
return determineTargetDataSource().getLoginTimeout();
}
@Override
public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
return determineTargetDataSource().getParentLogger();
}
}
这是在ThreadLocal 中保存查找键的类:
package com.custsoft.client;
/**
* Holds the client ID in the current thread. It is
* generally placed there by a REST filter that reads
* it from a "client" HTTP header.
*
* Created by eric on 8/25/15.
*/
public class ClientXrefHolder {
private static final ThreadLocal<String> CLIENT_XREF_HOLDER = new ThreadLocal<>();
public static String getClientXref() {
return CLIENT_XREF_HOLDER.get();
}
public static void setClientXref(final String clientXref) {
CLIENT_XREF_HOLDER.set(clientXref);
}
}
persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="customation" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!-- Only used when transaction-type=JTA -->
<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/customation)</jta-data-source>
<!-- Only used when transaction-type=RESOURCE_LOCAL -->
<non-jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/customation)</non-jta-data-source>
<class>com.custsoft.model.AccessToken</class>
<class>com.custsoft.model.JpaModel</class>
<class>com.custsoft.model.Role</class>
<class>com.custsoft.model.stats.Stat</class>
<class>com.custsoft.model.stats.StatDefinition</class>
<class>com.custsoft.model.User</class>
<class>com.custsoft.model.UserProperty</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
</properties>
</persistence-unit>
</persistence>