Ejb3 Basics: Bean Managed Transactions

I’m Lazy, why would I want to do my own transaction management?

While its true that the ejb3 container is usually pretty smart about persisting and handling transactions, its also not as smart as a real human being and probably isn’t able to handle complex database transactions and rollbacks. This is where bean managed transactions come in. By handling your own transactions you can avoid some major pitfalls.

The first problem with Container Managed Transactions (CMT) is there’s a time limit imposed by the container (although it’s a configurable timeout). If you are performing a lengthy task and are persisting the results (like FTPing files over to a third party vendor, persisting the results of a jms queue distribution, or committing across two different data sources – What the heck is a datasource?) then you will want to have control over when the bean commits the transactions. This frees you from a time constraint and also allows you to decide when you want to commit and what rollback strategy you want to use for error management. If you stick to using CMT during these kinds of database transactions, the container will invariably end up flipping out and start throwing all kinds of errors because out of the box the container is usually set to handle short duration transactions (like 5 minutes). Likewise, if a transaction is taking upwards of say, 30 seconds, it might be a sign that the application’s relationship with the database might not be as efficient as possible.

In a perfect world, CMT management streamlines the process for quick and easy transactions like selects, inserts and updates; the type that have all the data ready to insert and don’t have any weird secondary transaction dependencies that need to be completed before committing or inserting. For other weird transaction management handling, user Bean Managed Transactions like this:

 * set up the transaction maangement style for this class
public class MyClass... {

	// set the session context reference, we're going to use it soon
	@Resource private SessionContext sessionContext;

	public void setReminder(ReminderForm reminder) {

		UserTransaction utx = sessionContext.getUserTransaction();  
		try {  

			// begin the transaction

			 * persist your object

			// attempt to commit the transaction

		} catch(Exception e) {

			// something went wrong, lets try to rollback the transaction
			log.error("problem with the database transaction: " + 



Using those SessionContext objects with this type of construct you can create as many transactions as you want. The container’s transaction management is hands off during the entire body of this class, but you can override individual method with the CMT flag if you must.

Comments (4)

  1. 3:15 PM, January 17, 2010Andrew Lee Rubinger  / Reply

    In JBoss EJB3, CMT transaction timeouts are a default that are configurable via our vendor-specific APIs:


    Also, take care when using UserTransaction.rollback; in most cases I find that you’d instead be better off setting the Thread context to rollback at the appropriate boundary (utx.setRollbackOnly()). Else you may have other code in the call stack expecting a Tx association w/ the current Thread error out.

  2. 3:54 PM, January 17, 2010Ant  / Reply

    Awesome! Thanks for the clarification, I’ll update the example with the right method.

  3. 4:39 AM, February 11, 2011Carl  / Reply

    from the javaEE spec:
    Methods Not Allowed in Bean-Managed Transactions

    Do not invoke the getRollbackOnly and setRollbackOnly methods of the EJBContext interface in bean-managed transactions. These methods should be used only in container-managed transactions. For bean-managed transactions, invoke the getStatus and rollback methods of the UserTransaction interface.

    So if I understand correctly setRollbackOnly is not allowed in the catch…

    • 11:08 AM, February 11, 2011Ant  / Reply

      That’s what I thought too. I think Andrew was talking about a different boundary later on in the application. I assumed he was talking about right here in the bmt exception context. I guess the problem he’s talking about is more along the lines when you have a mixed case of BMT and CMT. I’ll update the example and make a note.

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pingbacks (0)

› No pingbacks yet.