在我的项目中,我同时使用了JPA和spring-jdbc-template&NamedParameterTemplate。 我需要两者都获得交易支持。
那么如何为JPA和spring-JDBC都配置@Transactional?
这是我用来配置它们的2个类。
类编号1:PersistenceConfiguration 这是声明应启用事务管理的类。
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.google.product.repository"},
excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.google.product.repository.mongoRepository.*.*Repository"))
public class PersistenceConfiguration {
@Autowired
private DataSource dataSource;
@Autowired
private Properties entityManagerProperties;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan("com.google.product.model");
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(entityManagerProperties);
return em;
}
}
类编号2:ProdDatabaseConfiguration 这是声明将bean重新标记为JDBC&JPA的类。例如primaryJdbcTemplate,secondaryJdbcTemplate和EntityManagerProperties。
@Configuration
@PropertySource({"classpath:database-prod.properties"})
@Profile("prod")
public class ProdDatabaseConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(ProdDatabaseConfiguration.class);
@Value("${jdbc.jndiName}")
private String jndiName;
@Value("${hibernate.dialect}")
private String hibernateDialect;
@Value("${hibernate.show_sql}")
private String hibernateShowSql;
@Value("${hibernate.cache.use_second_level_cache}")
private String hibernateSecondLevelCache;
@Value("${hibernate.cache.use_query_cache}")
private String hibernateQueryCache;
@Value("${jadira.usertype.databaseZone}")
private String databaseZone;
@Value("${jadira.usertype.javaZone}")
private String javaZone;
@Value("${mongo.jndiName}")
private String mongoJndiName;
@Bean
public DataSource dataSource() {
JndiDataSourceLookup jndiDataSourceLookup = new JndiDataSourceLookup();
return jndiDataSourceLookup.getDataSource(jndiName);
}
@Bean(name = "entityManagerProperties")
public Properties additionalProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "none");
hibernateProperties.setProperty("hibernate.dialect", hibernateDialect);
hibernateProperties.setProperty("hibernate.show_sql", hibernateShowSql);
hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", hibernateSecondLevelCache);
hibernateProperties.setProperty("hibernate.cache.use_query_cache", hibernateQueryCache);
hibernateProperties.setProperty("jadira.usertype.databaseZone", databaseZone);
hibernateProperties.setProperty("jadira.usertype.javaZone", javaZone);
return hibernateProperties;
}
@Bean(name = "primaryJdbcTemplate")
public JdbcTemplate primaryJdbcTemplate() {
JndiDataSourceLookup jndiDataSourceLookup = new JndiDataSourceLookup();
return new JdbcTemplate(jndiDataSourceLookup.getDataSource(jndiName));
}
@Bean(name = "secondaryJdbcTemplate")
public NamedParameterJdbcTemplate secondaryJdbcTemplate() {
JndiDataSourceLookup jndiDataSourceLookup = new JndiDataSourceLookup();
return new NamedParameterJdbcTemplate(jndiDataSourceLookup.getDataSource(jndiName));
}
答案 0 :(得分:2)
由于只使用一个数据源,因此可以删除所有配置,而仅使用spring.datasource属性。
事务也将立即可用,因为您只有此数据源。
在官方文档中了解有关此主题的更多信息:
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-sql