[jboss-user] [EJB 3.0] - BMT does not work the expected way.

sumantkb do-not-reply at jboss.com
Thu Jul 26 07:43:32 EDT 2007


I am trying to persist data through a BMT. The code is as shown below: 
 package com.titan.travelagent;

import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.transaction.UserTransaction;

@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class TravelAgentBean {

	private static EntityManagerFactory factory = Persistence
	.createEntityManagerFactory("titan");

	public void createCabin(Cabin cabin) {

		EntityManager manager = factory.createEntityManager();
		try {
			UserTransaction utx;
			try {
				utx = (UserTransaction) ((new InitialContext())
						.lookup("UserTransaction"));
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			utx.begin();
			manager.persist(cabin);
			utx.commit();

		} catch (Exception e) {

			e.printStackTrace();
		} finally {
			manager.close();
		}
	}

	

}



If I do "createCabin" it does not throw an exception , but I do not find the data persisted into the database. If I change the private static EntityManagerFactory factory = Persistence
	.createEntityManagerFactory("titan");
to
@PersistenceContext(unitName="titan")
     private EntityManager manager;

It starts working. But as far I understand @PersistenceContext(unitName="titan")
     private EntityManager manager; is supposed to be used for CMT. 

Could someone please help and clarify if any thing else is required.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4067769#4067769

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4067769



More information about the jboss-user mailing list