简体   繁体   English

使用2pc,休眠,Java EE,Jboss EAP访问多个数据库

[英]Accessing multiple databases using 2pc, hibernate, Java EE, Jboss EAP

I have an issue here and I must assume someone else must have run into this before. 我在这里有一个问题,我必须假设其他人之前也曾经遇到过这个问题。 It has been 3 weeks now and I have ran out of options nou. 到现在已经三个星期了,我已经没有选择权了。

Environment: 环境:

  • Jboss eap 7 Jboss EAP 7
  • Java 8 Java 8
  • Java EE 7 Java EE 7
  • Postgres Postgres的
  • Hibernate 5x 休眠5倍
  • Linux Linux的

The idea is to be able to write to two data sources in a single transaction using the xa-resource for my transaction management. 这个想法是能够使用xa-resource进行我的事务管理,从而在单个事务中写入两个数据源。 I have two databases both with different tables represented by different entities as such. 我有两个数据库,它们的表均由不同的实体表示。

My service bean is called by a managed bean from the war layer through an injected service interface at post construct. 我的服务Bean由War层中的受管Bean通过在构造后注入的服务接口调用。

My service implementation has an injected entity manager(em) annotated with the persistence context specifying the target database as a “unitName”. 我的服务实现具有一个注入的实体管理器(em),该实体管理器以持久性上下文注释,将目标数据库指定为“ unitName”。 The service bean calls the entity interface implementation passing the chosen em as a param, eventually the em is received by the base entity that in turn does the db operations. 服务bean调用实体接口实现,将选择的em作为参数传递,最终em由基础实体接收,而基础实体又执行db操作。

My application is divided into 3 modules/layers web=war, services=jar, and domain=jar with an EAR as the main archive. 我的应用程序分为3个模块/层:web = war,services = jar和domain = jar,其中EAR为主要存档。 My problem is actually in the back-end. 我的问题实际上是在后端。

I had first added in my persistence xml the following: 我首先在持久性xml中添加了以下内容:

  1. Created 2 persistence units (PU1 has 15 entities, PU2 has 17 listed in class element) 创建了2个持久性单元(PU1具有15个实体,PU2具有17个在类元素中列出的实体)
  2. transaction-type = JTA 交易类型= JTA
  3. exclude-unlisted-classes = false and 排除未列出的类= false和
  4. hibernate.hbm2ddl.auto is = update hibernate.hbm2ddl.auto是=更新

I have a base entity, entities and also have a repository package that holds my interfaces and their implementations to interact with my databases. 我有一个基础实体,也有一个存储库包,其中包含我的接口及其实现以与数据库交互。

Now when i run my setup like this, both my databases have exactly 32 tables each and my tables have some of the columns merged, ie I have table1 with fields a, b, and c in db1 then I also have table1 (same name) in db2 with fields d, e, and f and both the tables in their database end up with columns a, b, c, d, e, and f. 现在,当我以这种方式运行安装程序时,我的两个数据库每个都有正好32个表,并且我的表具有合并的某些列,即我在db1中具有带有字段a,b和c的table1,那么我也有table1(同名)在db2中,具有字段d,e和f,数据库中的两个表都以a,b,c,d,e和f列结尾。

If I manipulate my persistence.xml as below: 如果我按如下方式操作persistence.xml:

<persistence-unit name="PU1" transaction-type="JTA">
    <jta-data-source>java:jboss/datasources/db1</jta-data-source>
    <properties>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
    </properties>
    <class>domain.org.PemsBusinessArea</class>
    ...

</persistence-unit>
<persistence-unit name="PU2" transaction-type="JTA">
    <jta-data-source>java:jboss/datasources/db2</jta-data-source>
    <properties>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
    </properties>
    <class>domain.org .KeyFields</class>
    ...
</persistence-unit>

and I deploy and run my application I end up with the exception: 然后部署并运行我的应用程序,但最终出现以下异常:

Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2117)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1900)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1876)
at org.hibernate.loader.Loader.doQuery(Loader.java:919)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371)
at Caused by: org.postgresql.util.PSQLException: ERROR: column plexusbusi0_.name does not exist

and the column “name” exists in the target database table. 并且目标数据库表中存在“名称”列。

Is there anyone who can help me with the above situation? 有没有人可以帮助我解决上述情况? Thanks in advance. 提前致谢。

So it turns out I had missed specifying the target PU in one of my service bean methods. 因此,事实证明我错过了在一种服务bean方法中指定目标PU的问题。 After that change, all is now working fine. 更改之后,现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM