jpa - Creating an application managed JTA EntityManager -
i using glassfish 3.1.2, jpa2.0, eclipselink. trying create application managed entitymanager. transaction type persistence unit in persistence.xml file specified "jta"
<persistence-unit name="mypu" transaction-type="jta">
in bean create entitymanagerfactory as
entitymanagerfactory emf = persistence.createentitymanagerfactory("mypu");
and create enitymanager as
entitymanager em = emf.createentitymanager();
the question is: entity manager way jta? tried , able call gettransaction() method on entity manager without exception understanding should not allowed jta entity manager. if use entity manager in bean managed transaction (with entity manager being created after transaction begun) nothing gets persisted in db after commit on user transaction.
i know should have entity manager , entity manager factory injected understand behavior
the persistence.xml looks this:
<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="mypu" transaction-type="jta"> <provider>org.eclipse.persistence.jpa.persistenceprovider</provider> <jta-data-source>java:app/jdbc/mydatasource</jta-data-source> <class>example.myentity</class> <properties> <property name="eclipselink.ddl-generation.output-mode" value="sql-script"/> <property name="eclipselink.application-location" value="c:\gen-ddl"/> </properties> </persistence-unit> </persistence>
the glassfish-resource.xml file in ear project have defined datasource looks this:
<?xml version="1.0" encoding="utf-8"?> <!doctype resources public "-//glassfish.org//dtd glassfish application server 3.1 resource definitions//en" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd"> <resources> <jdbc-connection-pool name="mydb_pool" datasource-classname="oracle.jdbc.pool.oracledatasource" res-type="javax.sql.datasource"> <property name="url" value="jdbc:oracle:thin:@192.168.xxx.xxx:1521:xxx"/> <property name="user" value="xxx"/> <property name="password" value="xxx"/> </jdbc-connection-pool> <jdbc-resource enabled="true" jndi-name="java:app/jdbc/mydatasource" object-type="user" pool-name="mydb_pool"/> </resources>
check , include persistence.xml. using jta datasource or specifying jdbc connection?
if want use jta, must use jta datasource, otherwise need use resource_local.
if create entitymanager after jta transaction has started, should automatically join transaction. otherwise, need call jointransaction().
Comments
Post a Comment