简体   繁体   中英

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
  • Java 8
  • Java EE 7
  • Postgres
  • Hibernate 5x
  • 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. 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.

My service implementation has an injected entity manager(em) annotated with the persistence context specifying the target database as a “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.

My application is divided into 3 modules/layers web=war, services=jar, and domain=jar with an EAR as the main archive. My problem is actually in the back-end.

I had first added in my persistence xml the following:

  1. Created 2 persistence units (PU1 has 15 entities, PU2 has 17 listed in class element)
  2. transaction-type = JTA
  3. exclude-unlisted-classes = false and
  4. hibernate.hbm2ddl.auto is = update

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.

If I manipulate my persistence.xml as below:

<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. After that change, all is now working fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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