[jboss-user] [EJB 3.0] - Simple EJB3 Transaction Question

grdzeli_kaci do-not-reply at jboss.com
Mon Aug 7 08:48:43 EDT 2006


hi all,
i hava some problems with transactions again:
problem is that i want to manage them by myself not like this :
Session Bean 

  | @Remote(UserManager.class)
  | public @Stateless
  | class UserManagerBean implements UserManager {
  | 
  | 	@PersistenceContext(unitName = "Oracle")
  | 	private EntityManager oracleManager;
  | 
  | 	@TransactionAttribute(TransactionAttributeType.REQUIRED)
  | 	public UserRecord login(User userObject,UserContext context) throws MagtiException {
  | 	try {	
  | 	       oracleManager.persist(userObject);
  | 	} catch (Exception e) {
  | 		e1.printStackTrace();
  | }
  | }
  | 


then i did some changes :
I Version by some EJB3 Documentation

  | @Remote(UserManager.class)
  | public @Stateless
  | class UserManagerBean implements UserManager {
  | 
  | 	@Resource public UserTransaction utx;
  | 	@Resource public EntityManagerFactory factory;
  | 
  | 	@TransactionAttribute(TransactionAttributeType.REQUIRED)
  | 	public UserRecord login(User userObject,UserContext context) throws MagtiException {
  | 	try {	
  |                   utx.begin();
  | 	  oracleManager.persist(userObject);
  |                   utx.commit();
  | 	} catch (Exception e) {
  |                   utx.rollback();
  | 	  e1.printStackTrace();
  | }
  | }
  | 

but i get an error like this :


  | java.lang.IllegalStateException: Container UserManagerBean: it is illegal to inject UserTransaction into a CMT bean
  | 	at org.jboss.ejb3.injection.UserTransactionFieldInjector.<init>(UserTransactionFieldInjector.java:46)
  | 

if i remove this :
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
the problem was not resolved.

In II version program succsessfully deployed but i get an error again
Session Bean

  | @Remote(UserManager.class)
  | public @Stateless
  | class UserManagerBean implements UserManager {
  | 
  | 	@PersistenceContext(unitName = "Oracle")
  | 	private EntityManager oracleManager;
  | 
  | 	@TransactionAttribute(TransactionAttributeType.REQUIRED)
  | 	public UserRecord login(User userObject,UserContext context) throws MagtiException {
  | 	try {	
  | 	       oracleManager.getTransaction().begin();
  | 	       oracleManager.persist(userObject);
  | 	       oracleManager.getTransaction().commit();
  | 	} catch (Exception e) {
  | 	       oracleManager.getTransaction().rollback();
  | 		e1.printStackTrace();
  | }
  | }
  | 
in this case error was like this :


  | 03:42:07,015 ERROR [STDERR] java.lang.IllegalStateException: Illegal to call this method from injected, managed EntityManager
  | 03:42:07,015 ERROR [STDERR] 	at org.jboss.ejb3.entity.TransactionScopedEntityManager.getTransaction(TransactionScopedEntityManager.java:219)
  | 


ooo ya there is my persistance.xml file :

  | <?xml version="1.0" encoding="ISO-8859-1" ?>
  | 
  | <persistence xmlns="http://java.sun.com/xml/ns/persistence"
  | 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  | 	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
  | 	version="1.0">
  | 	<persistence-unit name="Oracle" transaction-type="JTA">
  | 		<jta-data-source>java:/XAOracleDS</jta-data-source>
  | 	<class>com.magti.businesslayer.ejb3entity.oracle.User</class>
  | 			<properties>
  | 			<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
  | 			<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
  | 			<property name="hibernate.connection.url" value="jdbc:oracle:thin:@192.168.9.133:1521:devnbtwo"/>
  | 			<property name="hibernate.connection.username" value="USER_MANAGER"/>
  | 			<property name="hibernate.connection.password" value="USER_MANAGER"/>
  | 			<property name="hibernate.default_schema" value="USER_MANAGER"/>			
  | 		</properties>
  | 	</persistence-unit>	
  | </persistence>
  | 


COULD ANYBODY HELP ME.
i don't know how i can manage transactions by myself :(


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

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



More information about the jboss-user mailing list