我一直在尝试使用spring boot配置openjpa-slice。配置如下共享:
@Configuration
public class DatasourceConfiguration extends JpaBaseConfiguration{
@Bean(name = "slice1")
public DataSource dataSource() {
JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
DataSource dataSource = dataSourceLookup.getDataSource("java:/MysqlXADS");
return dataSource;
}
@Bean(name = "slice2")
public DataSource dataSource2() {
JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
DataSource dataSource = dataSourceLookup.getDataSource("java:/MysqlXADS2");
return dataSource;
}
@Override
protected Map<String, Object> getVendorProperties() {
HashMap<String, Object> map = new HashMap<String, Object>();
return map;
}
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder) {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("openjpa.BrokerFactory", "slice");
properties.put("openjpa.slice.Names", "One, Two");
properties.put("openjpa.slice.Master", "Two");
properties.put("openjpa.slice.Lenient", "false");
properties.put("openjpa.slice.ConnectionDriverName", "com.mysql.jdbc.Driver");
properties.put("openjpa.slice.One.ConnectionFactoryName", "java:/MysqlXADS");
properties.put("openjpa.slice.Two.ConnectionFactoryName", "java:/MysqlXADS2");
properties.put("openjpa.slice.DistributionPolicy", "com.services.sample.configuration.UserDistributionPolicy");
properties.put("openjpa.slice.TransactionPolicy" , "xa");
properties.put("openjpa.TransactionMode" , "managed");
properties.put("openjpa.ConnectionFactoryMode", "managed");
properties.put("openjpa.jdbc.DBDictionary", "mysql");
properties.put("openjpa.Log","DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE");
return builder
.dataSource(dataSource()).jta(true)
.properties(properties)
.packages("com.services.sample.domain.entity")
.persistenceUnit("sample")
.build();
}
@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
OpenJpaVendorAdapter jpaVendorAdapter = new OpenJpaVendorAdapter();
jpaVendorAdapter.setShowSql(true);
return jpaVendorAdapter;
}
public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory")EntityManagerFactory entityManagerFactory){
JtaTransactionManager transactionManager = new JtaTransactionManager();
return transactionManager;
}
}
的persistence.xml
<persistence-unit name="sample" transaction-type="JTA" >
<description>generated-persistence-unit</description>
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class> ... </class>
</persistence-unit>
我已经使用wildfly服务器10配置了它。两个xa-datasource都指向不同的数据库。但是,当应用程序启动并执行查询时,它只会从传递给构建器的数据源中提取两次数据。似乎我的切片配置有问题。不确定在这种情况下是什么问题。