-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Is Hibernate post event listener useful for sending emails?
PostPosted: Thu Nov 03, 2016 2:54 pm 
Newbie

Joined: Sun Oct 16, 2016 1:22 pm
Posts: 2
I am using Spring Boot 1.3.2 and Spring Data JPA 1.9.4 with MS Sql server2014 database and has a requirement to send notification emails after post events like postInsert, postupdate and post delete. So i am following the approach to customize the JPA post event listeners like JpaPostInsertEventListener, JpaPostUpdatePostEventListener and JpaPostDeletePostEventListener and after override the OnPostUpdate, OnPostInsert and OnPostDelete methods.

In those methods i am sending notification emails, but i am not sure this approach is correct or not to use to send notifications after post events.
So can any one help to suggest the suitable way in hibernate or other approach to achieve the above requirment.?


Top
 Profile  
 
 Post subject: Re: Is hibernate post event listeners useful for post operations
PostPosted: Fri Nov 04, 2016 6:30 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Unless you're using JTA, your first approach is not reliable. What if you send the email and the currently running transaction rolls back?

Fortunately, you don't have to use JTA. You need to send the email only after the transaction has been committed. This is fairly easy to do since you need a non-transactional service to coordinate the first @Transactional Repository and the EmailService.

MyBusinessService - not-@Transactional
-> MyRepositoryService - @Transactional
-> MyEmailService not-@Transactional

This way, when the call exists the @Transactional Repository service, you are sure that the database transaction has been committed.

This way, the Email Service can send the email asynchronously, which is a good approach since it allows you to reduce the overall response time.

My friend, Michale Simons, already has an example for this on GitHub. Enjoy!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.