Running a statement for each transaction in Spring Boot - spring

I want to set a PostgreSQL transaction timeout for each Spring transaction like so:
SET LOCAL lock_timeout = '10s';
How can I extend Spring to do that?
I am not using Hiberante, but jOOQ, so setting a queryHint does not work in my case.

You could provide your own extended transaction manager and add the behaviour in the appropriate spot, probably in the begin method.

Related

Execute a method once every transaction begins inside methods annotated with spring - #Transactional

I want to make a database call before every method within my Spring application annotated with #Transactional started executing transaction.
There are few requirements for this:
This database call has to be part of the actual transaction that is about to begin.
The database connection used for the Transaction has to be same to be used for this database call and should setup some database data before 'real' transaction begins.
Is there something that Spring supports to achieve this goal ? One way I am thinking is to write own Aspect cloning more or less all Transactional code as is. Other way is to write Aspect. Not sure if there are more ways.

Hibernate Sessions in Quartz Job in Spring

TL;DR: Do I need to manually setup a Hibernate session around the Quartz job invocation?
I have quartz scheduler running in Spring but have very strange behaviour when it comes to using Hibernate in the Quartz job.
I have JPA repositories, and using these as below works as expected.
Reservation reservation = reservationRepo.findOne(resId);
However, when I try to use this entity as follows I get the subsequent exception
User owner = reservation.getRoom().getOwner();
Exception:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
If I set the spring logging to TRACE I see some JPA/Transaction/Thread magic, but it seems like it's all sorted out per-call to beans used, and no managed hibernate session exists on the Thread.
Do I need to somehow craft a Hibernate session around the Quartz job invocation?

Should one close Hibernate Session object every time after performing any CRUD operation?

I am working with enterprise application that uses Spring 3.x with Hibernate annotated programming.I am working with hibernate Session object for performing db operations in my XYZDaoImpl class.
And my doubt is "Is it correct way to close Hibernate Session object every time after performing any CRUD operation?"
If it is not the correct way, please advise me the recommended way of opening/closing of hibernate Session Object.
can you post a bit daoImpl code......
If you use sessionFactory.getCurrentSession(), you will get current session..in this case framework automatically flushed and closed when the transaction ends (commit or rollback).
If you use sessionFactory.openSession(), you have to manage the session yourself and to flush and close it "manually".
No, when using hibernate with spring, you should not (manually) open or close sessions, but let spring manage the hibernate session for you. Spring manages the session as a transactional resource, so you have to configure transaction management correctly.
If you open/close the hibernate session manually, you are hardly using the integration between these two frameworks: the session management is the main feature of the integration.
The only reason to open/close a hibernate session in a spring context is to use the same session to span multiple transactions.

Making specific method non transactional in Spring

I have a spring application which is based on Spring Batch. By default spring batch introduces transaction for its steps (i.e. at reader,writer and processor) . There are certain stages where I don't really need transaction to be enabled. Because transaction is enabled unnecessary for these methods its giving me some runtime errors as I am making call to two different databases in one method.
Is there any annotation which spring provides to DISABLE transaction for a specific set of methods ?
OR is there anything available in spring batch which can allow me to get rid of transaction either completely or declarative
I am even open to the solution which can disable transaction globally.
Any link , paper will greatly be appreciated.
Thanks in advance
Samir
Spring Batch is inherently transactional. Even if your datasources are not transactional, the semantics of the JobRepository require it. The closest you can get with Spring Batch and not being transactional is using the ResourcelessTransactionManager. This transaction manager is essentially a no-op transaction manager that just keeps track of if an operation is within the scope of a transaction or not.

spring 3 configuration without transaction manager

How to not use (disable) transaction manager in spring configuration?
I'm doing simple app that will add rows to database, so no need in any transactions.
So, is it possible configure in xml, not to create any transaction managers?
PS: i use hibernate if this matters anyhow.
If you don't configure a Transaction Manager, neither will Spring. Just don't add any <tx:> tags and don't configure any PlatformTransactionManager beans (in your case HibernateTransactionManager).

Resources