Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] COMMIT is not allowed in a subordinate session [message #529214]

The issue is probably related to you acquiring your EntityManager outside of
the JTA transaction. Unless you use a managed (injected) EntityManager, it
will be bound to only the JTA transaction that it was acquired in. You can
use joinTransaction() to join a new JTA transaction.

So either move your initEM after the begin, or call joinTransaction, or
inject the EM.

See,
http://en.wikibooks.org/wiki/Java_Persistence/Transactions#J oin_Transaction


njr wrote:
> 
> In My Servlet, I am using JTA transaction which is not a CMT.
> I am running this on Oracle XADatasource.
> 
> When I'm trying to perform the DB operation, I'm getting theĀ 
> Exception saying "COMMIT is not allowed in a subordinate session "
> 
> My Servlet code is :
> public class BatchServlet extends javax.servlet.http.HttpServlet
> implements
> 		javax.servlet.Servlet {
> 	PrintWriter out = null;
> 
> 	@PersistenceUnit(unitName="PERUNIT")
> 	EntityManagerFactory emf;
> 
> 	EntityManager em;
> 
> 	@Resource
> 	UserTransaction utx;
> 
> 	protected void doGet(HttpServletRequest request,
> 			HttpServletResponse response) throws ServletException, IOException {
> 		out = response.getWriter();
> 		try {
> 			initEM();
> 			
> 			utx.begin();
> 			updateDB();
> 			utx.commit();
> 
> 		} catch (Exception ex) {
> 			ex.printStackTrace(out);
> 			utx.rollback();
> 		} finally {
> 			closeEM();
> 		}
> 	}
> 	
> 	public void initEM() throws Exception {
> 		em = emf.createEntityManager();
> 	}
> 
> 	public void closeEM() {
> 		if (em != null) {
> 			em.close();
> 		}
> 	}
> 	
> 	private void updateDB() throws Exception {
> 		Address address = em.find(Address.class, 50030L);
> 		out.println("County :: " + address.getCounty());
> 		address.setBuildingNumber("Building # ABC");
> 		em.persist(address);
> 		out.println("Before Flush");
> 		em.flush();
> 		out.println("After Flush");
> 	}
> 
> 	public BatchServlet() {
> 		super();
> 	}
> }
> 
> 
> My Persistence.xml is
> <?xml version="1.0" encoding="UTF-8"?>
> <persistence version="1.0"
> 	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";>
> 	
> 	<persistence-unit name="PERUNIT" transaction-type="JTA">
> 		    <provider>
> org.eclipse.persistence.jpa.PersistenceProvider</provider> 
> 		    <jta-data-source>MYXADS</jta-data-source>
> 		    
> 		    <class> com.test.Address</class>
>             <properties>
> 	            <property name="eclipselink.logging.level" value="CONFIG"/>
> 	            <property name="javax.persistence.jtaDataSource"
> value="jdbc/MYXADS"/>
> 	            <property name="eclipselink.logging.level" value="FINE"/>
>             </properties>
> 	</persistence-unit>
> </persistence>
> 
> 
> Any inputs please
> 
> 


-----
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://old.nabble.com/COMMIT-is-not-allowed-in-a-subordinate-session--message--529214--tp28354781p28379967.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top