aspose file tools*
The moose likes Spring and the fly likes Can a MailSender participate in a transaction? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Can a MailSender participate in a transaction?" Watch "Can a MailSender participate in a transaction?" New topic
Author

Can a MailSender participate in a transaction?

Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Hello,
I would like for my mailAbonne method to be fully transactional. I noticed that even on a MailException being thrown the data is persisted by the dao (dao.persistMailingAbonnee(ma);). What's wrong with my configuration?
Can anyone please help?
Thanks in advance,
Julien.





Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Um, because you catch MailException, it will never throw MailException out of that method for the TransactionManager to roll it back.

If you catch it, you must re throw it if you want to release it out of the catch.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Mark Spritzler wrote:Um, because you catch MailException, it will never throw MailException out of that method for the TransactionManager to roll it back.

If you catch it, you must re throw it if you want to release it out of the catch.

Mark

Good point! I should have spotted that. However when I remove the try/catch the exception is no longer thrown... Any idea why?
regards,
J.
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Hello Mark,
I changed my method to public as you advised. Here is how it looks now:

I get the same behavior as before...
Any other idea?
J.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Do you have a

<tx:annotation-driven/> tag in your configuration to tell Spring you are using the @Transactional annotations and therefore Spring will create a Proxy around your object to add Transactionlity to it?

Thanks

Mark
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

OH, and that method is in your interface that that class implements?

Mark
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Mark Spritzler wrote:OH, and that method is in your interface that that class implements?

Mark

Yes it is indeed.
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Mark Spritzler wrote:Do you have a

<tx:annotation-driven/> tag in your configuration to tell Spring you are using the @Transactional annotations and therefore Spring will create a Proxy around your object to add Transactionlity to it?

Thanks

Mark

Yes it is too.
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
The problem seems to be that the timeout is set to indefinitely on my mailsender although I tried to set it to 1 second as follows:

and so the method hangs indefinitely. Hence the rollback is not performed...
Anyone any other idea how to sort this out?
Regards,
J.
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Sorry it does time out but the method still hangs indefinitely. Here is what the log says:

ERROR [18:13:24,990] (DesktopService.java:171) - org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.wanadoo.fr, port: 25;
nested exception is:
java.net.SocketTimeoutException: connect timed out. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: smtp.wanadoo.fr, port: 25;
nested exception is:
java.net.SocketTimeoutException: connect timed out; message exceptions (1) are:
Failed message 1: javax.mail.MessagingException: Could not connect to SMTP host: smtp.wanadoo.fr, port: 25;
nested exception is:
java.net.SocketTimeoutException: connect timed out

Why then won't it roll back the transaction??
Regards,
Julien.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

So it is throwing a MailSendException

But that isn't the Exception you are catching, unless MailSendException is a subclass of the Exception type that you are catching.

Mark
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Hi Mark,

I managed to isolate the problem. I now know it has nothing to do with the java mail api.

Here is my method:


I deliberately throw a RuntimeException from the envoyerMail method and it hangs where it is noted //here. I also know that Spring does create the beans properly and advises them propertly according to the @Transactional annotation because it says so in the logs. It is as if my method wouldn't manage to rethow the RuntimeException...
What could I be getting wrong?
Julien.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Try

Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Mark,
I tried as you advised. Same behaviour as before...

I don't know if that is relevant but here is how my method is called:






Please let me know if the fact that my bean is called from a swingworker matters or not.

Regards,
Julien.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can a MailSender participate in a transaction?