spring - Can I use one hibernate persistence unit on tomcat and share it between deployed wars? -


i have application running on tomcat have multiple wars.

  1. war1 web app used provision database.
  2. war2 uploading data via web service.
  3. war3 getting data via web service.
  4. war4 web app used view data in dashboard.

we use hibernate/jpa , spring. classes annotated , don't have persistence.xml of them. configuration in spring context file , annotations. here's spring file:

<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans"        xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"        xmlns:jpa="http://www.springframework.org/schema/data/jpa"        xmlns:aop="http://www.springframework.org/schema/aop"        xmlns:context="http://www.springframework.org/schema/context"        xmlns:flow="http://www.springframework.org/schema/webflow-config"        xmlns:lang="http://www.springframework.org/schema/lang"        xmlns:osgi="http://www.springframework.org/schema/osgi"        xmlns:tx="http://www.springframework.org/schema/tx"        xmlns:util="http://www.springframework.org/schema/util"        xmlns:p="http://www.springframework.org/schema/p"        xmlns:cache="http://www.springframework.org/schema/cache"        xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd           http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd           http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd           http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-3.0.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd           http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd           http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd ">     <tx:annotation-driven transaction-manager="transactionmanager"/>     <!-- directory scan repository classes -->     <jpa:repositories         base-package="com.blah.db.model.repository" />     <bean id="propertyconfigurer"           class="org.springframework.beans.factory.config.propertyplaceholderconfigurer">         <property name="locations">             <list>                 <value>/web-inf/configuration.properties</value>             </list>         </property>     </bean>     <cache:annotation-driven />         <bean id="cachemanager" class="org.springframework.cache.ehcache.ehcachecachemanager" p:cache-manager-ref="ehcache"/>         <bean id="ehcache" class="org.springframework.cache.ehcache.ehcachemanagerfactorybean"                     p:config-location="/web-inf/ehcache.xml"                     p:shared="true"/>     <context:component-scan base-package="com.blah.db.model.service" />     <context:component-scan base-package="com.blah.business.objects" />     <context:component-scan base-package="com.blah.data.access.objects" />     <context:component-scan base-package="com.blah.common" />      <bean class="org.springframework.orm.jpa.jpatransactionmanager"       id="transactionmanager">         <property name="entitymanagerfactory"             ref="entitymanagerfactory" />         <property name="jpadialect">             <bean class="org.springframework.orm.jpa.vendor.hibernatejpadialect" />         </property>     </bean>     <bean id="entitymanagerfactory"       class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean">         <property name="packagestoscan" value="com.blah.db.model"  />         <property name="datasource" ref="datasource" />         <property name="jpavendoradapter">             <bean class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter">                 <property name="generateddl" value="${hibernate.generate.ddl}" />                 <property name="database" value="${hibernate.database}" />                 <property name="showsql" value="${show.sql}" />             </bean>         </property>         <property name="jpaproperties">             <map>                 <entry key="hibernate.default_schema" value="${default.schema}"/>                 <entry key="hibernate.dialect" value="${jdbc.hibernate.dialect}"/>                 <entry key="hibernate.id.new_generator_mappings" value="${new.generator.mappings}"/>                 <entry key="hibernate.cache.region.factory_class" value="${hibernate.cache.factory.class}"/>                 <entry key="hibernate.cache.use_second_level_cache" value="${hibernate.use.second.level.cache}"/>                 <entry key="hibernate.cache.use_query_cache" value="${hibernate.use.query.cache}"/>                 <entry key="net.sf.ehcache.configurationresourcename" value="${ehcache.config.file}"/>             </map>         </property>     </bean>     <bean id="datasource"       class="org.apache.commons.dbcp.basicdatasource"       destroy-method="close">         <property name="driverclassname" value="org.postgresql.driver" />         <property name="url" value="jdbc:postgresql://127.0.0.1:5432/blah" />         <property name="username" value="postgres" />         <property name="password" value="password" />     </bean>     <bean id="opensessioninviewinterceptor" class="org.springframework.orm.jpa.support.openentitymanagerinviewinterceptor">         <property name="entitymanagerfactory">             <ref local="entitymanagerfactory"/>         </property>         <property name="jpaproperties">             <map>                 <entry key="singlesession" value="true"/>                 <entry key="flushmodename" value="flush_auto"/>             </map>         </property>     </bean> </beans> 

how ensure wars use single persistence unit? want able use ehcache, know can't use if use own pu.

is option moving javaee , packaging in ear?

thanks!

sharing persistance unit between applications means deploying common classes in shared tomcat folder, safe cleaning loaded shotgun tonge.

if need share cache, ehcache, use terracotta off heap cache. solution simples , less risky.

another option merge 4 apps bigger app, might make sense if of them share same data.


Comments

Post a Comment