【发布时间】:2020-01-28 11:52:50
【问题描述】:
我有一个在 Websphere Liberty 上运行的应用程序,它应该比较来自 2 个数据库/模式的表。
用户应该能够输入连接数据,例如主机和凭据。
我正在使用 Hibernate 访问应用程序数据库。
我尝试使用多个持久性单元,一个用于应用程序数据库,一个用于所有其他数据库。
但我遇到了两个问题:
- 我收到“非法尝试征用多个 1PC XAResources” 有时会出错
- 可以查询 2 个 DB 用户提交的凭据,但我没有得到任何结果,除非我连接 到 server.xml 文件中列出的与 DataSource 相同的数据库
这是服务器上 server.xml 上的 DataSources(dbs 是 oracle dbs)
<dataSource id="MyAppDS" jndiName="jdbc/MyDS" type="javax.sql.ConnectionPoolDataSource">
<jdbcDriver javax.sql.ConnectionPoolDataSource="oracle.jdbc.pool.OracleConnectionPoolDataSource" libraryRef="OracleSQLLib"/>
<connectionManager agedTimeout="30m" connectionTimeout="10s" maxPoolSize="20" minPoolSize="5"/>
<properties password="..." url="jdbc:oracle:thin:@...:1521:..." user="..."/>
</dataSource>
<dataSource id="OtherOracle" jndiName="jdbc/OtherOracle" type="javax.sql.ConnectionPoolDataSource">
<jdbcDriver javax.sql.ConnectionPoolDataSource="oracle.jdbc.pool.OracleConnectionPoolDataSource" libraryRef="OracleSQLLib"/>
<connectionManager agedTimeout="30m" connectionTimeout="10s" maxPoolSize="20" minPoolSize="5"/>
<properties password="..." url="jdbc:oracle:thin:@127.0.0.1:1521:XE" user="..."/>
</dataSource>
这是 EJB 模块上的 persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="main-persistence">
<jta-data-source>jdbc/MyDS</jta-data-source>
<class>classes...</class>
<properties>
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.Oracle9iDialect" />
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
</properties>
</persistence-unit>
<persistence-unit name="other-persistence" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>jdbc/OtherOracle</non-jta-data-source>
<class>classes...</class>
<properties>
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.Oracle9iDialect" />
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
</properties>
</persistence-unit>
在 Java Bean 上我使用 EntityManagerFactory
@PersistenceUnit(unitName = "other-persistence")
private EntityManagerFactory emf;
我使用这样的自定义凭据创建实体管理器
Map<String, String> properties = new HashMap<String, String>();
properties.put("hibernate.connection.driver_class", "oracle.jdbc.OracleDriver");
properties.put("hibernate.connection.url", myCustomCreatedConnectionUrl);
properties.put("hibernate.connection.username", customUser);
properties.put("hibernate.connection.password", customPassword);
properties.put("hibernate.dialect", "org.hibernate.dialect.Oracle9iDialect");
properties.put("hibernate.show-sql", "true");
EntityManager entityManager = emf.createEntityManager(properties);
如果我使用 getProperties 检查 EntityManager 属性,一切似乎都是正确的。但是只有当凭证/主机是 = 到数据源时,查询才有效。否则我没有结果(但没有错误)
可能是什么问题? 有没有办法只使用一个持久性单元,但针对不同的查询使用自定义主机/凭据?
【问题讨论】:
标签: java hibernate entitymanager websphere-liberty