I have a code in this format (java + spring):
@Service
public class mainService{
@Inject
private ServiceA a;
@Inject
private ServiceB b;
@Transactional
public void methodTest{
try{
System.out.println("Start");
a.insertIntoDbTableOne();
b.insertIntoDbTableTwo();
}catch(Throwable e){
e.printStackTrace();
System.out.println("This is the catch statement");
}finally{
System.out.println("this is finally");
}
}
}
In my action class, i am calling the mainService.java
by injecting it as a service also.
Both ServiceA
and ServiceB
methods called are annoted with @Transactional
as well.
The weird things is, when i run this code (i make it to throw error in ServiceA
method when it insert into db), the result sequence is not what i expect.
The result is:
1. "Start" is printed
2 Do stuff in insertIntoDbTableOne method (without inserting into db)
3. Do stuff in insertIntoDbTableTwo method (without inserting into db)
4. "this is finally" is printed
5. The system tries to insert the record the db which should be inserted in step 2 and hit error!
i think it is cause by the transactional annotation, but i tried to remove the transactional annotation in insertIntoDbTableOne
method, but it is not helping.
Any idea how to make the system catch this error within the try catch? i cannot afford to only catch it in the action class which calls this methodTest
.