首页 > 解决方案 > 每次在 Hibernate 中动态创建 EntityManager / 连接到自定义主机/数据库

问题描述

我有一个在 Websphere Liberty 上运行的应用程序,它应该比较来自 2 个数据库/模式的表。

用户应该能够输入连接数据,例如主机和凭据。

我正在使用 Hibernate 访问应用程序数据库。

我尝试使用多个持久性单元,一个用于应用程序数据库,一个用于所有其他数据库。

但我有两个问题:

  1. 我有时会收到“非法尝试征用多个 1PC XAResources”错误
  2. 可以使用用户提交的凭据查询 2 个数据库,但我没有得到任何结果,除非我连接到 server.xml 文件中列出的与 DataSource 相同的数据库

这是服务器上 server.xml 上的数据源(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 属性,一切似乎都是正确的。但是只有当凭证/主机是 = 数据源时,查询才有效。否则我没有结果(但没有错误)

问题可能是什么?有没有办法只使用一个持久性单元但为不同的查询使用自定义主机/凭据?

标签: javahibernateentitymanagerwebsphere-liberty

解决方案


关于第一个错误,““非法尝试登记多个 1PC XAResources”,这是因为您在同一个事务中使用这两种资源。我可以<non-jta-data-source>jdbc/OtherOracle</non-jta-data-source>在您的配置中看到,这表明您可能打算让 jdbc/OtherOracle 成为non-enlisting 资源。要使其工作,需要将数据源本身配置为 non-enlisting。您可以使用以下transactional="false"属性执行此操作:

<dataSource id="OtherOracle" jndiName="jdbc/OtherOracle" type="javax.sql.ConnectionPoolDataSource" transactional="false">
...

另一方面,如果您确实希望这两种资源都加入事务,那么您需要使用 XADataSource 而不是 ConnectionPoolDataSource。这是一个如何执行此操作的示例(请注意,必须为此更新type typeunderdataSource attribute & class under :jdbcDriver

<dataSource id="MyAppDS" jndiName="jdbc/MyDS" type="javax.sql.XADataSource">
    <jdbcDriver javax.sql.XADataSource="oracle.jdbc.xa.client.OracleXADataSource" 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.XADataSource">
    <jdbcDriver javax.sql.XADataSource="oracle.jdbc.xa.client.OracleXADataSource" 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>

在第二个问题中,我认为您是说不同的用户看不到数据。这可能是因为不同的数据库用户使用不同的模式并且无法访问彼此的数据吗?如果您可以使用通用模式获取所有用户,那么添加@Table(schema="YOUR_SCHEMA_NAME")到 JPA@Entity可能会有所帮助。可以在此处找到 Table 注释的 JavaDoc 。


推荐阅读