Using #Transactional in Micronaut without using Hibernate - jdbc

Micronaut has support for JDBC, and the guide states that #Transactional from micronaut-spring can be used for AOP style transaction handling. I can't get this to work, when using #Transaction with a plain datasource, I get an exception that no TransactionManager is in place.
Do anyone know if I can use plain JDBC with micronaut with #Transactional support?

The reason for this is the DataSource needs to be wrapped in a TransactionAwareDataSourceProxy in order to perform #Transaction
U can try to add smth like this
compile "io.micronaut:spring"
runtime "org.springframework:spring-jdbc"

Related

Spring #Transactional - synchronize via AspectJ

I am trying to synchronize declarative transactions (i.e. methods annotated with #Transactional) using AspectJ like so:
...
import org.aspectj.lang.annotation.Aspect;
...
#Component
#Aspect
public class TransactionMonitor extends TransactionSynchronizationAdapter {
#Before("execution(#org.springframework.transaction.annotation.Transactional * *.*(..))")
private void registerTransactionSynchronizationOnAnnotation(JoinPoint joinPoint) {
TransactionSynchronizationManager.registerSynchronization(this);
}
}
This currently fails with java.lang.IllegalStateException: Transaction synchronization is not active which indicates that the synchronization is not run inside the transaction execution, but before. I want to ensure that this is the other way round, of course.
I found this answer, however #Order(Ordered.LOWEST_PRECEDENCE) had no effect, and
#DeclarePrecedence(
"org.springframework.transaction.aspectj.AnnotationTransactionAspect, xxx.xxx.TransactionMonitor, *"
)
led to this during startup:
java.lang.IllegalArgumentException: DeclarePrecedence not presently supported in Spring AOP
I have the feeling this is AOP and AspectJ not being happy with each other, but I am not sure. I am thankful for any ideas.
EDIT: I have to use #EnableTransactionManagement(proxyTargetClass = true), can this be related to the issue?
For #DeclarePrecedence you need to switch to native AspectJ. Spring AOP is just "AOP lite" and technologically has little in common with AspectJ other than its syntax which is basically an AspectJ subset. The Spring manual describes how to use native AspectJ in Spring via LTW (load-time weaving). Precedence declaration for Spring components rather works using #Order, BTW.
I am not a Spring user at all, but as for declarative transaction management, it already knows proxy-based Spring AOP versus native AspectJ mode, see EnableTransactionManagement.mode and the enum constants in AdviceMode. Besides, EnableTransactionManagement also has an order property. Reading Javadoc and the Spring manual helps, I guess.

How to use ObjectDB properly in spring boot application?

I have a spring boot application with objectdb embedded database.
I am manually handling connection and transaction operations as described at http://www.objectdb.com/java/jpa/persistence/overview
Below is a sample code that I am using: (taken from objecdb documentation):
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("myDbFile.odb");
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
// Operations that modify the database should come here.
em.getTransaction().commit();
}
finally {
if (em.getTransaction().isActive())
em.getTransaction().rollback();
}
It works but the code has become uggly since I had to use try catch finally blocks in order to properly close connections.
I want to refactore my application so that database operations are done in JpaRepositories or Dao classes with #Transactional methods (as described in http://spring.io/guides/gs/accessing-data-jpa/)
I had a research on the web but could not find any solution that works.
What I am looking for is a very simple spring boot sample application with:
spring-boot-starter-data-jpa
Objectdb (embedded)
Maven
Uses annotation based configuration (no xml file)
A dummy entity class (e.g: Customer(id,firstname) )
A JpaRepository class or dao class with list() and #Transactional persist(Customer) methods
Note: I already tried this post but could not make it work.
ObjectDB support answered my question
https://www.objectdb.com/forum/2328#item-6

why `EntityManager` work,but EntityManagerFactory did not work for me?

I try to use Spring+JPA+Hibernate and try to inject EntityManagerFactory,and later create EntityManger in my code.But when I use entityManager.persist(user),the user not saving to the database.But when I try to inject the EntityManager instead of EntityManagerFactory,it worked !,I do not know where is the problem.
you can also see this question for more code.
When using a plain EntityManagerFactory instead of an EntityManager you need to call createEntityManager. This will always create a new EntityManager, this is basically a plain EntityManager not managed nor detected by Spring. So you will also have to start/commit transactions yourself.
When using the EntityManager you will obtain a transactional synchronized instance, which is managed by Spring and bound to the current transaction. So no need to start / commit a transaction yourself.
See also the JPA section of the reference guide.

How do I change my configuration to a different data source?

I went through the Data Access With Spring tutorial and the in memory database they use in step 3 is working. But, I'm not clear on what I need to add/change to get it to query my development (Oracle) database now?
I want to use Hibernate, do I still need this JPAConfiguration class or would I have something Hibernate specific?
Please don't just post a link to the Hibernate reference. I'm reviewing that as well, but since I'm also using Spring, it's not clear to me the proper way to load the hibernate.cfg.xml and inject the Hibernate session in that context.
Don't be blocked by the fact that the class is called JPAConfiguration. You need to understand what the class does. Note that it has the annotation #Configuration which you can use along with AnnotationConfigApplicationContext to produce a Spring bean context.
That functionality is described in the Spring documentation for The IoC container.
What you need to change is how your DataSource and EntityManagerFactory beans are created. You'll need to use a DataSource that gets Connection instances from a JDBC Driver that supports Oracle databases.

What's the difference between using #Transactional and Spring template?

If I use #Transactional in my DAO will all of my EntityManager queries be encapsulated with commit and close? Or do I need to use Spring template (JPA template, Hibernate template)? What's the difference between using #Transactional and Spring template?
The difference between using annotation-based transaction demarcation (#Transactional) and the TransactionTemplate is that TransactionTemplate couples you to Spring's transaction infrastructure and means that you will programmatically handle setting the transaction status if the transaction should be rolled back. You can use annotation-based transaction demarcation with the Spring transaction support or with AspectJ transactions outside of a Spring container.
See also the online documentation for transactions in Spring.
The Spring template classes are only there to provide a nicer API for doing persistence operations - they do not deal with transactions. If you want to have transactional operations, you either need to use the #Transactional annotation approach, or use TransactionTemplate.
When you use #transactional with the proper Spring configuration, Spring will recognize that the method needs an transaction and will handle the transaction creation, commit and close for you.
Like skaffman said, #transactional is not directly tied to the template classes. They can be used for any class that may need transactions.
do u mean usin #transactional will encapsulate my dao methods with commit,close or when using spring transaction template (jpatemplate, hibernatetemplate) ?

Resources