spring - 我可以在 tomcat 上使用一个 hibernate 持久性单元并在部署的 war 之间共享它吗?

标签 spring hibernate tomcat jpa persistence-unit

我有一个在 tomcat 上运行的应用程序,我们有多个 war 。

  1. War1 是一个用于配置数据库的网络应用程序。
  2. War2 用于通过网络服务上传数据。
  3. War3 用于通过网络服务获取数据。
  4. War4 是另一个用于在仪表板中查看数据的网络应用程序。

我们使用 hibernate/jpa 和 spring。这些类是带注释的,我们没有任何一个的 persistence.xml。所有配置都在 spring 上下文文件和注释中。 这是 Spring 文件:

<?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 to scan for 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>

如何确保所有 war 都使用一个持久化单元?我希望能够使用 ehcache,但我知道如果他们都使用自己的 PU,我将无法使用它。

我唯一的选择是转向 JavaEE 并将其全部打包到 EAR 中吗?

谢谢!

最佳答案

在应用程序之间共享持久化单元意味着将您的公共(public)类部署在共享的 tomcat 文件夹中,这就像用舌头清洁上膛的霰弹枪一样安全。

如果你需要共享缓存,使用ehcache,使用terracotta作为堆外缓存。此解决方案将很简单,而且风险绝对较小。

另一种选择是将这 4 个应用程序合并为一个更大的应用程序,如果它们都共享相同的数据,这可能有意义。

关于spring - 我可以在 tomcat 上使用一个 hibernate 持久性单元并在部署的 war 之间共享它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12941265/

相关文章:

java - "IllegalArgumentException occurred calling getter of"使用 SINGLE_TABLE 继承策略运行条件时

java - zk中电子表格的协作编辑

java - JDBC 瘦适配器无法在高峰时间建立连接

spring - 如何使用 Spring 捕获 Thymeleaf( View )渲染异常?

java - Hibernate标准,从实体A中选择,其中someId在B中

java - org.hibernate.hql.internal.ast.QuerySyntaxException : <table_name> is not mapped [from <table_name>]

java - 如何在 Windows 下的 Java 服务中添加时间戳 GC 日志文件名?

java - 在 Spring 中排除单元测试 @Configurations 的最佳方法?

java - cf 推送失败 Java 版本不匹配

java - 在独立 Java 应用程序中使用现有的 GORM 数据模型