Java Spring引导:手动配置连接
我正在用SpringBoot开发一个应用程序,它需要连接到两个不同的数据库。为此,我必须手动配置连接。 起初一切似乎都很好,但后来我意识到我在交易中遇到了问题。 事实上,尽管我在方法上使用了事务性注释,但我意识到我在DB上写的每一条记录都会立即提交,如果抛出异常,则不会发生回滚。 交易似乎不活跃 这是我对第一个数据库的实际配置Java Spring引导:手动配置连接,java,spring,hibernate,spring-boot,spring-transactions,Java,Spring,Hibernate,Spring Boot,Spring Transactions,我正在用SpringBoot开发一个应用程序,它需要连接到两个不同的数据库。为此,我必须手动配置连接。 起初一切似乎都很好,但后来我意识到我在交易中遇到了问题。 事实上,尽管我在方法上使用了事务性注释,但我意识到我在DB上写的每一条记录都会立即提交,如果抛出异常,则不会发生回滚。 交易似乎不活跃 这是我对第一个数据库的实际配置 import java.util.HashMap; import javax.sql.DataSource; import org.springframework.bea
import java.util.HashMap;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.jta.JtaTransactionManager;
@Configuration
@EnableConfigurationProperties
@EnableJpaRepositories(
entityManagerFactoryRef = "fooEntityManager",
basePackages = "xx.yy.zz.repository"
)
@EnableTransactionManagement
public class FooConfig {
@Autowired
private Environment env;
@Value("${spring.datasource.primary.jndi-name}")
private String jndiConnection;
@Primary
@Bean(destroyMethod = "")
public DataSource ds() {
JndiDataSourceLookup lookup = new JndiDataSourceLookup();
return lookup.getDataSource(jndiConnection);
}
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean fooEntityManager(DataSource ds) {
LocalContainerEntityManagerFactoryBean em
= new LocalContainerEntityManagerFactoryBean();
em.setDataSource(ds());
em.setPackagesToScan(
new String[] { "xx.yy.zz.entity" });
HibernateJpaVendorAdapter vendorAdapter
= new HibernateJpaVendorAdapter();
vendorAdapter.setShowSql(true);
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", "none");
properties.put("hibernate.dialect",
env.getProperty("spring.jpa.properties.hibernate.dialect"));
properties.put("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName());
properties.put("hibernate.implicit_naming_strategy", SpringImplicitNamingStrategy.class.getName());
em.setJpaPropertyMap(properties);
return em;
}
@Primary
@Bean(name="transactionManager")
public PlatformTransactionManager fooTransactionManager() {
JpaTransactionManager transactionManager
= new JpaTransactionManager();
transactionManager.setEntityManagerFactory(
fooEntityManager(ds()).getObject());
return transactionManager;a
}
}
import java.util.HashMap;
导入javax.sql.DataSource;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Value;
导入org.springframework.boot.context.properties.EnableConfigurationProperties;
导入org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy;
导入org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
导入org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.context.annotation.Primary;
导入org.springframework.core.env.Environment;
导入org.springframework.data.jpa.repository.config.EnableJpaRepositories;
导入org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
导入org.springframework.orm.jpa.JpaTransactionManager;
导入org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
导入org.springframework.orm.jpa.vendor.hibernatejbavendorapter;
导入org.springframework.transaction.PlatformTransactionManager;
导入org.springframework.transaction.annotation.EnableTransactionManagement;
导入org.springframework.transaction.jta.JtaTransactionManager;
@配置
@EnableConfigurationProperties
@授权代理(
entityManagerFactoryRef=“fooEntityManager”,
basePackages=“xx.yy.zz.repository”
)
@启用事务管理
公共类FooConfig{
@自动连线
私人环境署;
@值(${spring.datasource.primary.jndi name}”)
私有字符串JNDI连接;
@初级的
@Bean(方法=”)
公共数据源ds(){
JndiDataSourceLookup lookup=新的JndiDataSourceLookup();
返回lookup.getDataSource(jndiConnection);
}
@豆子
@初级的
公共LocalContainerEntityManager(数据源ds){
LocalContainerEntityManagerFactoryBean em
=新的LocalContainerEntityManagerFactoryBean();
em.setDataSource(ds());
em.setPackagesToScan(
新字符串[]{“xx.yy.zz.entity”});
冬眠盲蝽
=新的HibernateJavaEndorapter();
vendorAdapter.setShowSql(true);
em.setjpavendor适配器(供应商适配器);
HashMap属性=新建HashMap();
properties.put(“hibernate.hbm2ddl.auto”、“none”);
properties.put(“hibernate.dial”,
getProperty(“spring.jpa.properties.hibernate.dial”);
properties.put(“hibernate.physical\u naming\u策略”,SpringPhysicalNamingStrategy.class.getName());
properties.put(“hibernate.implicit_naming_策略”,SpringImplicitNamingStrategy.class.getName());
em.setJpaPropertyMap(属性);
返回em;
}
@初级的
@Bean(name=“transactionManager”)
公共平台交易管理器FootTransactionManager(){
JpaTransactionManager事务管理器
=新的JpaTransactionManager();
transactionManager.setEntityManagerFactory(
fooEntityManager(ds()).getObject();
返回事务管理器;返回事务管理器
}
}
我还尝试使用JTA,但这次相反,应用程序从未在DB上编写过。
这是jta配置
import java.util.HashMap;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.jta.JtaTransactionManager;
@Configuration
@EnableConfigurationProperties
@EnableJpaRepositories(
basePackages = "xx.yy.zzzz.repository",
entityManagerFactoryRef = "fooEntityManager"
)
@EnableTransactionManagement
public class FooConfig {
@Value("${spring.datasource.primary.jndi-name}")
private String jndiConnection;
@Autowired
private Environment env;
@Primary
@Bean(destroyMethod = "")
public DataSource ds() {
JndiDataSourceLookup lookup = new JndiDataSourceLookup();
return lookup.getDataSource(jndiConnection);
}
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean fooEntityManager(DataSource ds) {
LocalContainerEntityManagerFactoryBean em
= new LocalContainerEntityManagerFactoryBean();
em.setJtaDataSource(ds());
em.setPackagesToScan(
new String[] { "xx.yy.zz.entity" });
HibernateJpaVendorAdapter vendorAdapter
= new HibernateJpaVendorAdapter();
vendorAdapter.setShowSql(true);
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", "none");
properties.put("hibernate.dialect",
env.getProperty("spring.jpa.properties.hibernate.dialect"));
properties.put("hibernate.transaction.jta.platform",
env.getProperty("org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"));
properties.put("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName());
properties.put("hibernate.implicit_naming_strategy", SpringImplicitNamingStrategy.class.getName());
em.setJpaPropertyMap(properties);
return em;
}
@Primary
@Bean(name="transactionManager")
public PlatformTransactionManager fooTransactionManager() {
JtaTransactionManager transactionManager = new JtaTransactionManager();
return transactionManager;
}
}
import java.util.HashMap;
导入javax.sql.DataSource;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Value;
导入org.springframework.boot.context.properties.EnableConfigurationProperties;
导入org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy;
导入org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
导入org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.context.annotation.Primary;
导入org.springframework.core.env.Environment;
导入org.springframework.data.jpa.repository.config.EnableJpaRepositories;
导入org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
导入org.springframework.orm.jpa.JpaTransactionManager;
导入org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
导入org.springframework.orm.jpa.vendor.hibernatejbavendorapter;
导入org.springframework.transaction.PlatformTransactionManager;
导入org.springframework.transaction.annotation.EnableTransactionManagement;
导入org.springframework.transaction.jta.JtaTransactionManager;
@配置
@EnableConfigurationProperties
@授权代理(
basePackages=“xx.yy.zzzz.repository”,
entityManagerFactoryRef=“fooEntityManager”
)
@启用事务管理
公共类FooConfig{
@值(${spring.datasource.primary.jndi name}”)
私有字符串JNDI连接;
@自动连线
私人环境署;
@初级的
@Bean(方法=”)
公共数据源ds(){
JndiDataSourceLookup lookup=新的JndiDataSourceLookup();
返回lookup.getDataSource(jndiConnection);
}
@豆子
@初级的
公共LocalContainerEntityManager(数据源ds){
LocalContainerEntityManagerFactoryBean em
=新的LocalContainerEntityManagerFactoryBean();
em.setJtaDataSource(ds());
em.setPackagesToScan(
新字符串[]{“xx.yy.zz.entity”});
冬眠盲蝽
=新的