Sandy
Sandy

Reputation: 21

Send mail on insertion of a record in DB

I have a table recording trade data . Whenever a record is inserted in this table I would like to send a mail with the details of that record.

Any pointers on how that can be done. I believe working on a trigger that is fired on insert on that table is the way to go but am not sure about what to do further for sending the mail .

Imp to note is that since its a trade table the performance of the query or the database should not be impacted.

Upvotes: 2

Views: 1252

Answers (2)

Eelke
Eelke

Reputation: 22013

You could make some background task that looks at the table at a regular interval and sends emails for any new records it finds. New records could be detected efficiently by using a serial or a timestamp column.

Often best to run this task from some scheduler that starts it every n minutes.

Upvotes: 1

Goran Jovic
Goran Jovic

Reputation: 9508

Here you have an explanation how to send a mail from Java: How do I send an e-mail in Java?

Now, first try to find a method call responsible for saving that object in database. Then add email sending code to the method.

Put that code outside, more specifically after the transaction - you can't rollback a sent email anyway. Just make sure you get a response from database that transaction was successful.

And please note that by sending an email for each new record you may easily bury the email inbox with these messages.

Upvotes: 2

Related Questions