File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can a MailSender participate in a transaction?

 
Julien Martin
Ranch Hand
Posts: 384
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Julien Martin
Ranch Hand
Posts: 384
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OH, and that method is in your interface that that class implements?

Mark
 
Julien Martin
Ranch Hand
Posts: 384
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Spritzler wrote:OH, and that method is in your interface that that class implements?

Mark

Yes it is indeed.
 
Julien Martin
Ranch Hand
Posts: 384
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try

 
Julien Martin
Ranch Hand
Posts: 384
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic