相关疑难解决方法(0)

在Java EE应用程序中处理多个EntityManager

我有大约10个EntityManagers的Java EE应用程序(EM的数量可能会增加).我的应用程序还包含许多无状态,有状态和消息驱动的bean.

@PersistenceContext我可能会将所有内容存储在一个单独的bean中,并使用其他bean访问它,而不是在每个bean中注入我的EM (以及两种方法来检测用户使用哪个EM).像这样,不用担心可维护性.

然而,将EM存储在一个单独的bean中是否是线程安全的?瓶颈会出现吗?

另一个解决方案是创建一个抽象类,所有bean都将扩展它.

什么是更好的解决方案?

java jpa entitymanager concept

5
推荐指数
2
解决办法
1万
查看次数

spring jpa hibernate有更多的数据源

我必须在我的应用程序(spring)中使用两个不同的数据库,使用Hibernate,Jpa.我想直接将不同的表定义到不同的数据源.所以我使用两个不同的持久性单元,我尝试使用

<property name="packagesToScan" value="it.two.app.domain.first" />
Run Code Online (Sandbox Code Playgroud)

<property name="packagesToScan" value="it.two.app.domain.second" />
Run Code Online (Sandbox Code Playgroud)

将不同的表放入不同的包中.但它不起作用.事实上,所有表都是第一个数据源.然后我试着写入持久化XML文件类的名称

 <persistence-unit name="persistenceFirst" transaction-type="RESOURCE_LOCAL">
 <class>it.two.app.domain.first.OneTable</class>
 <exclude-unlisted-classes/>
</persistence-unit>
Run Code Online (Sandbox Code Playgroud)

和it.two.app.domain.second.OtherTable

但是当我运行Log时,表'firstDB.other-table'不存在,我用到了services文件中

@PersistenceContext(unitName ="persistenceFirst")
private EntityManager em;
Run Code Online (Sandbox Code Playgroud)

@PersistenceContext(unitName = "persistenceSecond")
EntityManager em;
Run Code Online (Sandbox Code Playgroud)

你有什么想法吗?Thi是数据源XML文件

<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<!-- first datasource -->
<context:property-placeholder location="classpath:jdbc-first.properties"/>
<bean id="dataSourceFirst"  class="org.apache.commons.dbcp.BasicDataSource"
    p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}"
    p:username="${jdbc.username}" p:password="${jdbc.password}"  />
<!-- second datasource -->      
<context:property-placeholder location="classpath:jdbc-second.properties"/>
<bean id="dataSourceSecond"     class="org.apache.commons.dbcp.BasicDataSource"
    p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}"
    p:username="${jdbc.username}" p:password="${jdbc.password}"  /> …
Run Code Online (Sandbox Code Playgroud)

spring hibernate jpa persistence-unit

3
推荐指数
1
解决办法
7852
查看次数