Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Transactions in Message Driven Beans (newbie doubt) HELP!  RSS feed

Pablo Fernandez
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, this is my first post in JavaRanch, although I've used it many times as a resource for my work
Currently I am working in a new implementation of the email sending in my project. I'll try to be clear in what's my question.

The emails I send all include data that needs to be "harvested" from several objects and be included as parameters in a sendEmail method call.
I now have this functionality literally everywhere. I send emails from different beans in different parts of the system, and in every place that I need to send one, I have to gather all the info before calling my sendMail method. it looks something like this:

String patient = appointment.getPatient().getNameForEmail();
String patient_email = appointment.getPatient().getAccount().getEmail();
String doctor = appointment.getPracticePhysician().getProfile().getNameForEmail();
String new_time = Utils.formatTime(appointment.getScheduledToTime());
String new_date = Utils.formatDateMiddle(appointment.getScheduledToDate());
int hours = appointment.getPracticePhysician().getPractice().getAppointmentCancelDuration();
float discount = appointment.getDiscountAmount();
float total_cost = appointment.getSummaryCost();

mail.sendMail(Constants.DEFAULT_MAIL_SENDER, patient_email, null, "mail.appointment_scheduled", new Object[] {doctor},
new Object[]{patient, doctor, new_time, new_date, Utils.formatMoney(total_cost), Utils.formatMoney(discount), hours});

The idea now is to move all this functionality into a message driven bean, and let it handle all the required processes.
I'll go with an example to explain my doubt.

I have my AppointmentManagerBean, which handles all the appointment creation, update, cancelation, etc. This is related of course with a Patient object, with a Physician object, and with other...

I'd like to send either the whole Appointment object, or its id, to the MDB and do all the data gathering there, but I'm not sure how it would handle the following situation:

I have transaction A in the appointment bean, then I make a call to a method in message driven bean and send it the appointment object and start transaction B.
Now I have parallel transactions, but let's say in trans A something changes in the appointment (or in other of the possibly referenced objects) before trans B is finished, how do I keep consistency between the instances that the message driven bean has of the object, and the changes produce in trans A ??

I realize by re reading that I made quite a mess, but I can't find an easier way to explain it.

I'll be extremely thankful to anyone who can give me a hand or point me in the right direction.

See you!


ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to send the Appointment object.

As for the transaction part, the client side transaction (where you post the message to the jms server) can include only the post to the messaging server. For example if you are inserting to the db and also posting to the jms server, you can configure that these two activities are in the scope of one transaction. You would need a distributed transaction for the scenario I outlined.

You cannot link the delivery of the message (and what the mdb does as part of message processing) in the client transaction. If at all, there is such a dependency, you will have to code that yourself - this is often referred to as compensating transactions.

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!