How can I query SQL immediately after opening a transaction?
As in the title. I need to query a specific function in the database that will define multiple values ββassigned to a transaction. If possible, I want to make it a global config associated with a specific profile (There are a few requests that I don't want to load this feature).
The project is built on Java SE 1.7, Spring Boot 1.1.7 and connects to PostgreSQL database.
Requests are created at three levels: SomeClassController (Controller), SomeClassService (Service), SomeClassDB (Repository). In SomeClassDB, it connects to the database using a JdbcTemplate from Spring and performs CRUD operations. Before any of these operations, I want to request a function. And as I mentioned before, I don't need a method that will do the job. Do I need something like a global configuration on the TransactionManager?
Maybe I should be using TransactionSynchronization with the beforeCommit method? But I don't know how to use it globally.
EDIT1: what can I, but I don't want;):
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
@Service
public class SessionService {
Boolean flag;
public SessionService(){
flag=false;
}
@Value("${appVersion}") String appVersion;
@Value("${appArtifactId}") String appArtifactId;
public void addSession(){
if(!flag){
jdbcTemplate.execute("SELECT add_ses('"+appArtifactId+"','"+appVersion+")");
flag=true;
}
}
public void deleteSession(){
jdbcTemplate.execute("SELECT del_ses()");
}
}
And now I can just call these two methods at the beginning and end of the second class of the class with @Autowired this class. But I really don't want to do this. Someone, someday will forget this while propagating this 2nd level class SomeClassService and I want to avoid it.
Hope this brings you closer to my problem.
source to share
This might be a typical use case for aspects . Have you tried writing an aspect for this? An aspect can be configured to execute some code before and after calling a method in a specific package or method decorated with a specific annotation. Spring has very good aspect support.
Another way is to create a proxy object (using java.lang.reflect.Proxy) that will call your initialization code and then pass it to the proxy object.
source to share