Friday, 15 March 2013

java - JPA with local resource instead of JTA datasource -



java - JPA with local resource instead of JTA datasource -

i building simple web application utilize jpa. although expect deploying on glassfish think may beneficial manage entity persistence within application rather through jta datasource. acknowledge may not bright idea?

some time ago set next (possibly lost web reference):

public class persistencemanager { private static final persistencemanager instance = new persistencemanager(); protected entitymanagerfactory emf; public static persistencemanager getinstance() { homecoming instance; } private persistencemanager() { } public entitymanagerfactory getentitymanagerfactory() { if (emf == null) { createentitymanagerfactory(); } homecoming emf; } public void closeentitymanagerfactory() { if (emf != null) { emf.close(); emf = null; } } protected void createentitymanagerfactory() { this.emf = persistence.createentitymanagerfactory("met"); } }

so, questions; reasonable approach - there pitfalls here?

i've deployed jpa without container managed persistence. i've used atomikos jta transaction manager. simplest way not utilize jndi based lookup. recomment using spring's jpatransactionmanager , configure either persistence.xml or spring-xml.

persistence manager in spring-xml:

<bean id="pum" class="org.springframework.orm.jpa.persistenceunit.defaultpersistenceunitmanager"> <property name="defaultdatasource" ref="datasource" /> <property name="persistencexmllocations"> <list> <value>classpath:meta-inf/etf-persistence.xml</value> </list> </property> </bean> <bean id="emf" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean"> <property name="persistenceunitmanager" ref="pum" /> <property name="persistenceunitname" value="my-pu" /> <property name="jpaproperties"> <props> <prop key="hibernate.ejb.cfgfile">my-hibernate.cfg.xml</prop> <prop key="hibernate.session_factory_name">mysessionfactory</prop> <prop key="hibernate.dialect">org.hibernate.dialect.oracle10gdialect</prop> <prop key="hibernate.archive.autodetection">none</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.format_sql">false</prop> </property> </bean>

don't specify non-jta or jta-data-source tags in persistence.xml. rest of setup standard-spring (entitymanagerfactory, datasource etc.).

java java-ee jpa

No comments:

Post a Comment