我有两个配置文件,如下所示:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = { "repo" },
entityManagerFactoryRef = "db1",
transactionManagerRef = "JpaTxnManager_db1")
public class RepositoryConfigSpringDataDb1 {
}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = { "repo" },
entityManagerFactoryRef = "db2",
transactionManagerRef = "JpaTxnManager_db2")
public class RepositoryConfigSpringDataDb2 {
}
我有一个 dao 类,它有很多方法。现在在 dao 类中,我认为我可以使用事务指定的 @Transactional 注释来访问特定的数据库。
调用 db1 的一些示例方法是:
@Transactional(transactionManager="JpaTxnManager_db1")
public List<EntityOne> getAllEntitiesById(String id) {
return entityOneRepo.findById(id);
}
调用 db2 的其他一些方法是:
@Transactional(transactionManager="JpaTxnManager_db2")
public List<EntityOne> getAllEntitiesById(String id) {
return entityOneRepo.findById(id);
}
存储库的定义如下:
@org.springframework.stereotype.Repository
public interface EntityOneRepository extends PagingAndSortingRepository<EntityOne, String> {
// ommitted for brevity
--我已经为这些定义了不同的数据源,但我定义的第二个数据源没有被命中。
知道我缺少什么吗?
是否可以使用扩展 PagingAndSortingRepository 的同一个 EntityOneRepository 来基于 transactionManagerRef 和entityManagerFactoryRef 访问 2 个不同的数据库?
最佳答案
我之前多次遇到过这个问题,并使用 Spring 的 DelegatingDataSource 解决了它。它允许您定义多个 DataSource对象并通过某种类型的查找键委托(delegate)给所需的正确目标数据源。对于您在帖子中显示的代码来说,它的一个子类可能是一个不错的选择,可能是 TransactionAwareDataSourceProxy正如 JavaDoc 描述的第一句话所述:
Proxy for a target JDBC DataSource, adding awareness of Spring-managed transactions. Similar to a transactional JNDI DataSource as provided by a Java EE server.
我通常总是在任何给定线程中使用相同的目标数据源,因此我倾向于将查找键塞入ThreadLocal对象,并让代理数据源在每次调用 DataSource#getConnection 时读取此数据以查找实际的目标数据源已制作完成。
如果您创建这样的委托(delegate)(代理)数据源,您的 EntityManagerFactory可以使用它作为其底层 JDBC 数据源,并在任何给定时间根据需要委托(delegate)给正确的目标数据源。
我多年来一直在 JPA 代码中使用这种类型的方法,其中我需要使用相同的持久性单元访问多个数据源,这对我来说非常有用。也应该可以很好地与 Spring JPA 数据存储库配合使用。
下面是我之前参与的一个项目的一些实现代码。该代码属于我,因此请随意复制您喜欢的任何内容,它可以按照您的意愿使用它。
这里是委托(delegate)给实际目标 JDBC DataSource
的代理 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);
}
}
持久性.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>
关于java - 是否可以使用同一个 Spring Data Repository 来访问两个不同的数据库(数据源)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59275989/