0

I am using Spring with jpa with the following configuration:

@Configuration
public class JPAConfiguration
{
    protected static final String PERSISTENCE_UNIT_NAME = "persistenceUnit";

    @Autowired
    private DatabaseConfigurationProperties databaseConfigurationProperties;

    @Bean
    public JpaTransactionManager createJPATransactionManager(EntityManagerFactory emf)
    {
        JpaTransactionManager jtManager = new JpaTransactionManager();
        jtManager.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
        jtManager.setEntityManagerFactory(emf);
        return jtManager;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean createEntityManagerFactoryBean(DataSource dataSource)
    {
        LocalContainerEntityManagerFactoryBean lcemfb = new LocalContainerEntityManagerFactoryBean();
        lcemfb.setDataSource(dataSource);
        lcemfb.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
        lcemfb.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        lcemfb.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
        return lcemfb;
    }

    @Bean
    public DataSource getDataSource()
    {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(databaseConfigurationProperties.getDriver());
        dataSource.setUrl(databaseConfigurationProperties.getUrl());
        dataSource.setUsername(databaseConfigurationProperties.getUser());
        dataSource.setPassword(databaseConfigurationProperties.getPassword());
        return dataSource;
    }
}

I have a DAO which injects the entitymanager as follow:

@PersistenceContext(unitName = "persistenceUnit")
protected EntityManager entityManager;

Somehow, when I use this DAO in threads, the data of the entitymanager is not synchronized. In Thread A i persist an Entity. After that I try to read it in Thread B and it is not present (even when I du a flush on Thread A). For the transaction handling I use the spring @Transactional annotation.

What is the correct way to get spring jpa with hibternate thread safe? I read alot of tutorials, but they all kinda use the same code as me.

thanks alot.

  • Possible duplicate of [Is EntityManager really thread-safe?](https://stackoverflow.com/questions/24643863/is-entitymanager-really-thread-safe) – isank-a Oct 08 '19 at 06:45
  • A part of my description is a duplicate of your link. But what I am looking for is the right solution for this problem ;) – user3922628 Oct 08 '19 at 06:53
  • Well, if a class is not thread-safe, the solution is not to use it across multiple threads. It would be easier if you described the *actual* problem you have (i.e. why you think you need to span the transaction across multiple threads) – crizzis Oct 08 '19 at 20:35
  • @crizzis I have not a Transaction which is across several Threads, i have a Transaction in each thread. But the Data of the finished transactions is not synchronized – user3922628 Oct 09 '19 at 10:33
  • One thing I don't quite follow is the following part: 'In Thread A i persist an Entity. **After that** I try to read it in Thread B'. If those two threads execute in parallel, how do you know what happens when? It would be easier if you provided more context, including the service methods doing the persisting and reading, as well as how the threads are being created/executed. – crizzis Oct 09 '19 at 15:36
  • *even when I du a flush on Thread A*. Flushing != transaction commit. The transaction is only committed when the transactional method returns. If B attempts to read before A has committed then, assuming a transaction isolation level of READ_COMMITTED, B will not see changes in A's, as yet, uncommited changes. – Alan Hay Oct 09 '19 at 17:17

0 Answers0