In Spring 3.0, are scheduled task methods annotated with #Scheduled already transactional? If not, will annotating the same method as #Transactional work? That is, will the scheduler honour the transaction annotation?
As far I known, the answer is no, #Scheduled doesn't care about transactions. Add #Transactional manually, it should work (also you may check it yourself by enabling debug messages and watching to log).
Related
We have a legacy JEE-application with a modernized spring core. Sometimes, calls are made from the JEE part into Spring. When deployed on the JEE-Server, a configured JTA-Transactionmanager handles the transactional issues sufficiently.
In integration tests outside a real JEE-Container this is not the case. When we start a transaction outside the Spring core and call a #Transactional method from within the core, the transaction gets used all-right but after terminating the #Transactional method, Spring commits the transaction (a behaviour not seen when using JTA).
Is there any way to configure Spring such that #Transactional checks for a running transaction and only terminates transactions it opend itself?
I have an application which uses Spring 4.3 and Hibernate 5.3.
There's a web application with a presentation layer, a servive layer and a DAO layer, as well as some jobs sharing the same service and DAO layers.
Transactions are initialized in different layers with #Transactional annotations.
It led me to a problem I described here: Controlling inner transaction settings from outer transaction with Spring 4.3
I read a bit about how to set-up transactions to wire Spring and Hibernate together. It looks the recommended approach is to initialize transactions in the service layer.
What I don't like is that most transactions exist only because they are required for hibernate to work properly.
And when I really need a transaction for a job calling multiple service methods, it seems I don't have a choice to keep initializing transactions from the jobs. So moving #Transactional annotations from DAO to service doesn't seem to make any difference.
How would you recommend to set-up transactions for this kind of application?
Pardon me for replying in answer as I am not able to comment
I don't get the meaning of you having to keep initializing transactions from the jobs?
Usually for
DAO class, it should be annotated with #Repository.
Service class with #Service and #Transactional
Webservice, if u have, with #RestController, #RequestMapping, #Transactional.
By doing so, any call from service class will be 1 transaction thus if Service class A calls service B and C, even if service class C throws error, the whole transactions will be rollback.
Annotated spring beans in my spring-cloud application are being created twice. I assume this is because they get constructed into the bootstrap context and then into a child application context.
For me this is undesirable because some are annotated with #Scheduled to perform periodic refreshes of the data that they provide from a backend data source and this is happening twice in quick succession once for each context.
If it's not otherwise harmful then can I disable all of the application beans from being created in the bootstrap context? If not then can I detect in code when I'm running in the bootstrap context? I use entirely annotation-based beans with component scanning enabled on the Camden SR4 release.
OK I solved this myself. It was down to two different issues in the code and nothing to do with multiple contexts.
Firstly my long held assumption that an #PostConstruct method is only ever called once was false. Moving my one-off bean initialisation code to an implementation of ApplicationListener<ApplicationReadyEvent> solved that one.
Secondly I got bit by multiple initialisation of a bean with an #Scheduled annotation causes the scheduler to run multiple times. To be fair, this behaviour is noted in the documentation. This was easily solved by moving the scheduled task creation to a regular java ScheduledExecutorService set up in the ApplicationReadyEvent implementation.
#Scheduled to execute the method only once at the time of deployment by using fixedRate, in the spring application..
Please let me know, I am unable to fix it.
Thanks in advance.
Use the #PostConstruct annotation so the method will execute once after the bean's creation. Running the method only once defeats the purpose of the #Scheduled annotation.
Why to use #Scheduled then? You can use InitializingBean or #PostConstruct to execute your process.
In my application I have 2 beans which have methods annotated with #Scheduled annotation. Sometimes I need to schedule both the methods, and sometimes I need to schedule either one of them, based on the input arguments to the application. How can I disable the #Scheduled method after it has been loaded? I am using Spring 3.1 .
i would suggest instead of using #Schedule you should use TaskScheduler to schedule your job based on user input, this way you will have more control over execution, different implementation is provided by spring refer to javadoc and scheduling doc
You can inject ThreadPoolTaskScheduler into your application and call
taskScheduler.getScheduledExecutor().shutdown();
But remember, this is a hack. So I suggest using TaskScheduler directly without #Scheduled annotations.