java entity数据库连接_java - 每次在Hibernate中动态创建EntityManager /连接到自定义主机/数据库 - SO中文参考 - www.soinside.com...

我有一个在Websphere Liberty上运行的应用程序,该应用程序应该比较2个数据库/方案中的表。

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

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

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

但是我有两个问题:我得到“非法尝试征募多个1PC XAResources”有时会出现错误

可以查询两个数据库用户提交的凭据,但除非连接,否则我没有任何结果到server.xml文件中列出的与DataSource相同的数据库

这是服务器上server.xml上的数据源(dbs是oracle dbs)

这是EJB模块上的persistence.xml<?xml version="1.0" encoding="UTF-8"?>

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">

jdbc/MyDS

classes...

value="org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform" />

value="org.hibernate.dialect.Oracle9iDialect" />

value="false" />

jdbc/OtherOracle

classes...

value="org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform" />

value="org.hibernate.dialect.Oracle9iDialect" />

value="false" />

在Java Bean上,我使用EntityManagerFactory@PersistenceUnit(unitName = "other-persistence")

private EntityManagerFactory emf;

并且我使用这样的自定义凭据创建实体管理器Map properties = new HashMap();

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属性,一切似乎都是正确的。但是,仅当凭据/主机为数据源=时查询才有效。否则我没有结果(但没有错误)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值