jpa - Using Nativequery to delete/update records in JTA -
i stuck in hard situation moment.
the problem is: have use nativequery delete and/or update db records in jta context (ejb).
my jpa persistence.xml looks like:
<persistence-unit name="ozsscjpanbpu" transaction-type="jta"> <provider>org.eclipse.persistence.jpa.persistenceprovider</provider> <jta-data-source>jdbc/postgres_ozssc</jta-data-source> <mapping-file>com/longz/ozssc/model/postcodeentity.xml</mapping-file> <class>com.longz.ozssc.model.customerentity</class> ......
if use delete statement in way:
@override public void remove(smsdoutboxentity toberemoved){ em.createnativequery("delete outbox \"id\" = " + toberemoved.getid()).executeupdate();
transactionrequiredexception thrown:
root cause javax.persistence.transactionrequiredexception: exception description: no transaction active
such there no transaction existing.
if use transaction manually as:
@override public void remove(smsdoutboxentity toberemoved){ em.gettransaction().begin(); em.createnativequery("delete outbox \"id\" = " + toberemoved.getid()).executeupdate(); /*em.flush();*/ em.gettransaction().commit();
illegalstateexception thrown:
root cause javax.ejb.ejbexception: ejb exception: ; nested exception is: java.lang.illegalstateexception: method public abstract javax.persistence.entitytransaction javax.persistence.entitymanager.gettransaction() cannot invoked in context of jta entitymanager.
seems can't use transaction manually, jta manage transaction self.
so question is: how can use native query delete/update records in jta managed context?
please advise.
for jta transaction provided container , should not create yourself. obtain transaction in jta, have access resource:
@resource public usertransaction utx; @resource public entitymanagerfactory factory; @override public void remove(smsdoutboxentity toberemoved){ entitymanager em = factory.createentitymanager(); try { em.createnativequery("delete outbox \"id\" = " + toberemoved.getid()).executeupdate(); utx.commit(); catch (runtimeexception e) { if (utx != null) utx.rollback(); throw e; // or display error message } { em.close(); }
if want omit part have commit/rollback transaction yourself, need have transaction managed container ejb3. can use stateless/stateful ejb beans.
update:
for weblogic, try use user weblogic.transaction.usertransaction instead of javax.transaction.usertransaction. doc says:
this interface defines weblogic-specific extensions javax.transaction.usertransaction.
Comments
Post a Comment