How to use JPA CMT with glassfish 3.1 -


i new jpa , want try out simple examples on glassfish 3.1. able run sample programs bean managed transactions. when trying sames examples cmt, getting transaction required exception.

unable clue going wrong.

my project simple web project , included jpa facet entity generation.

may reason not using ejb project unable features of ejb container of glassfish app server....not sure this...

my persitence.xml below:

<?xml version="1.0" encoding="utf-8"?> <persistence version="2.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_2_0.xsd"> <persistence-unit name="jpapractise" transaction-type="jta">     <jta-data-source>jdbc/jpaconnpool</jta-data-source>     <class>model.address</class>     <class>model.employee</class> </persistence-unit> 

normal servlet trigger point:

@webservlet("/testjpa") public class testjpa extends httpservlet { private static final long serialversionuid = 1l;  @persistenceunit entitymanagerfactory emf; /*@resource  usertransaction tx;*/   protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {     entitymanager em = emf.createentitymanager();      employee e1 = new employee();     e1.setemployeeid("290874");     e1.setname("shashi shankar");     e1.setsalary(60000);     try{         //tx.begin();         em.persist(e1);         em.flush();         //tx.commit();     }catch(exception e){         e.printstacktrace();     }    }  /**  * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response)  */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {     // todo auto-generated method stub } 

}

please me know real issue.

with bmt, responsible managing transactions, while in cmt it's container's. therefore example working bmt.

testjpa servlet & not enterprise bean container manage transactions.

from documentation :

in enterprise bean container-managed transaction demarcation, ejb container sets boundaries of transactions. can use container-managed transactions type of enterprise bean: session, or message-driven. container-managed transactions simplify development because enterprise bean code not explicitly mark transaction’s boundaries. code not include statements begin , end transaction.

you can try below code.

try{     em.gettransaction().begin();`      em.persist(e1);     em.gettransaction().commit();`. }catch(exception e){     e.printstacktrace(); }finally{     em.close(); } 

also, have change persistence.xml have resource_local transaction.

<persistence-unit name="jpapractise" transaction-type="resource_local">         <non-jta-data-source>jdbc/jpaconnpool</non-jta-data-source> 

Comments