Integration of JOPA transactions into the Spring declarative transactions - the @Transactional
annotation.
- Isolation level configuration is currently not supported, because it is not supported by JOPA either.
- When a method is not transactional, the library with create an
EntityManager
instance, which will be automatically closed after one operation/query execution.
Usage of the library is fairly simple. It is only necessary to instantiate the JopaTransactionManager
and DelegatingEntityManager
Spring beans.
JopaTransactionManager
implements Spring'sTransactionManager
interface and Spring calls it at significant moments of the transaction lifecycle (begin, commit etc.),DelegatingEntityManager
implements JOPA'sEntityManager
interface and is injected into transactional Spring beans, where it delegates calls to theEntityManager
instance bound to the current transaction. This is the only class with which the end users directly interact.
Do not forget to EnableTransactionManagement
.
Assuming there is an EntityManagerFactory
Spring bean, Java-based configuration of the aforementioned beans looks
for example as follows:
@Configuration
@EnableTransactionManagement
@Import(PersistenceFactory.class)
public class PersistenceConfig {
@Bean
public DelegatingEntityManager entityManager() {
return new DelegatingEntityManager();
}
@Bean(name = "txManager")
public PlatformTransactionManager transactionManager(EntityManagerFactory emf,
DelegatingEntityManager emProxy) {
return new JopaTransactionManager(emf, emProxy);
}
}
EntityManager
is then injected into beans using Autowired
or Inject
(PersistenceContext
is currently not supported,
as it is closely tied to JPA):
@Autowired
private EntityManager em;
The tests contain a complete setup. For a fully operational example, see JOPA Example 04 - JOPA + Spring and the JSON-LD demo.
A full-fledged information system using JOPA and declarative Spring transactions is TermIt.
The library is now available in Maven central, so getting it is just a matter of adding a Maven dependency:
<dependency>
<groupId>com.github.ledsoft</groupId>
<artifactId>jopa-spring-transaction</artifactId>
</dependency>
See https://github.com/kbss-cvut/jopa.
MIT