Answer the question
In order to leave comments, you need to log in
Why might entities not be saved in the database?
I have a task to read in one database (db2) and write to another (oracle).
spring boot jpa, hibernate
I have configured two Datasources, two EntityManagerFactory, two PlatformTransactionManager. for each base.
I also have a set of Entity classes for the second database, and for each I have my own Repository extends JpaRepository.
Actually the setup works. Since the application can certainly read in the database - the data source and update the jdbcTemplate data there with the component. In another database - where you need to write, the application can also read. I see in the traces requests to the database (for example, to count the number of records in the table) of the findById method of the repository.
But then some kind of garbage begins when you try to write through the repositories. The save(entity) method is called, I see in the hibernate log that the entity has been added to the queue.
22-01-21 Fr 20:15:16.830 DEBUG [actSaveEventListener] : Generated identifier: component[]{}, using strategy: org.hibernate.id.CompositeNestedGeneratedValueGenerator
22-01-21 Fr 20:15:16.830 TRACE [actSaveEventListener] : Saving [com.batch.vamos2dwh.writer.persistence.dwh.model.StF...
22-01-21 Fr 20:15:16.830 TRACE [ActionQueue ] : Adding an EntityInsertAction for [com.batch.vamos2dwh.writer.persistence.dwh.model.StF...] object
22-01-21 Fr 20:15:16.830 TRACE [ActionQueue ] : Adding insert with no non-nullable, transient entities: [EntityInsertAction[com.batch.vamos2dwh.writer.persistence.dwh.model.StF...]
22-01-21 Fr 20:15:16.830 TRACE [ActionQueue ] : Adding resolved non-early insert action.
@Bean(name = "chainedTransactionManager")
public ChainedTransactionManager transactionManager(@Qualifier("dwhTransactionManager") PlatformTransactionManager ds1,
@Qualifier("vamosTransactionManager") PlatformTransactionManager ds2) {
return new ChainedTransactionManager(ds1, ds2);
}
@Transactional(value="chainedTransactionManager", propagation = Propagation.REQUIRES_NEW)
public int processSCFZ() {
getting transaction for blablaEntityRepository.save
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = "com.batch.vamos2dwh.writer.persistence.dwh.repository",
entityManagerFactoryRef = "dwhEntityManagerFactory",
transactionManagerRef = "dwhTransactionManager")
public class DwhDatasourceConfiguration {
@Value("${dwh.jpa.properties.hibernate.dialect}")
private String hibernateDialect;
@Bean(name = "dwhDatasource")
@ConfigurationProperties(prefix = "dwh.datasource")
@Primary
public DataSource dwhDatasource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "dwhTransactionManager")
@Primary
public PlatformTransactionManager dwhTransactionManager(@Qualifier("dwhDatasource") DataSource ds) {
return new DataSourceTransactionManager(ds);
}
@Bean(name = "dwhJdbc")
public JdbcTemplate vamosJdbc(@Qualifier("dwhDatasource") DataSource ds) {
return new JdbcTemplate(ds);
}
@Bean(name = "dwhEntityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean dwhEntityManagerFactory(
EntityManagerFactoryBuilder entityManagerFactoryBuilder,
@Qualifier("dwhDatasource") DataSource dwhDatasource) {
HashMap<String, Object> props = new HashMap<>();
props.put("hibernate.dialect", hibernateDialect);
return entityManagerFactoryBuilder
.dataSource(dwhDatasource)
.properties(props)
.packages("com.batch.vamos2dwh.writer.persistence.dwh")
.persistenceUnit("dwhPersistence")
.build();
}
}
Answer the question
In order to leave comments, you need to log in
My mistake was
@Bean(name = "dwhTransactionManager")
@Primary
public PlatformTransactionManager dwhTransactionManager(@Qualifier("dwhDatasource") DataSource ds) {
return new DataSourceTransactionManager(ds);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question