- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个需要访问两个不同数据库服务器(H2 和 Oracle)的 webapp。
容器是 Apache Tomee 1.5.1我正在使用 Java EE 堆栈及其中提供的库(JSF、JPA、CDI、EJB 等)。
我试图在 XA 事务中使用两个实体管理器从 Oracle 数据库中提取数据,并在转换后将其保存在 H2 中,但无论我使用什么实体管理器,所有查询都是针对 H2 数据库执行的。有什么帮助吗?
编辑 :我发现如果我尝试以相反的顺序访问实体管理器,它们的行为是相同的,但访问 Oracle。即:实体管理器留在访问的第一个数据库中。
发生这种情况的 EJB(从 JSF 调用 service.getFoo()
):
@Named
@Stateless
public class Service {
@Inject
@OracleDatabase
private EntityManager emOracle;
@Inject
@H2Database
private EntityManager emH2;
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public List<Foo> getFoo() {
TypedQuery<Foo> q = emH2.createQuery(
"SELECT x FROM Foo f", Foo.class);
List<Foo> l = q.getResultList();
if (l == null || l.isEmpty()) {
update();
}
return q.getResultList();
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void update() {
// FAIL: This query executes against H2 with Oracle entity manager!
List<Object[]> l = emOracle.createNativeQuery("SELECT * FROM bar ").getResultList();
//more stuff...
}
}
public class Resources {
@Produces
@PersistenceContext(unitName = "OraclePU")
@OracleDatabase
private EntityManager emOracle;
@Produces
@PersistenceContext(unitName = "H2PU")
@H2Database
private EntityManager emH2;
}
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="H2PU"
transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>H2DS</jta-data-source>
<class>my.app.h2.Foo</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
<persistence-unit name="OraclePU" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>OracleDS</jta-data-source>
<class>my.app.oracle.Bar</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
</persistence>
<Resource id="OracleDS" type="javax.sql.DataSource">
jdbcDriver = oracle.jdbc.xa.client.OracleXADataSource
jdbcUrl = jdbc:oracle:thin:@server:port:instance
jtaManaged = true
password = abcde
userName = user
</Resource>
<Resource id="H2DS" type="javax.sql.DataSource">
jdbcDriver=org.h2.jdbcx.JdbcDataSource
jdbcUrl=jdbc:h2:h2/db;AUTO_SERVER=TRUE
jtaManaged = true
password = edcba
userName = user
</Resource>
最佳答案
容器管理的持久性上下文
当使用容器管理的持久性上下文时(就像通过 @PersistenceContext 注释一样),JPA 规范指定只有一个持久性上下文可以与 JTA 事务相关联。
持久性上下文由 Java EE 容器创建。尽管出现了代码(@PersistenceContext 注释似乎表明 PC 被直接注入(inject)到您的 EntityManager 实例变量中),但持久性上下文实际上作为引用存储在 JTA 事务中。每次发生 EntityManager 操作时,它都不会引用它自己的内部持久性上下文。相反,它执行一个特殊的操作,因为它是容器管理的——它总是在 JTA 事务中查找持久性上下文并使用它。这称为 JTA 持久性上下文传播。
JPA 规范中的一些引用:
When a container-managed entity manager is used, the lifecycle of the persistence context is always managed automatically, transparently to the application, and the persistence context is propagated with the JTA transaction.
Container-managed Transaction-scoped Persistence Context
... A new persistence context begins when the container-managed entity manager is invoked[76] in the scope of an active JTA transaction, and there is no current persistence context already associated with the JTA transaction. The persistence context is created and then associated with the JTA transaction.
Container-managed Extended Persistence Context
... A container-managed extended persistence context can only be initiated within the scope of a stateful session bean. It exists from the point at which the stateful session bean that declares a dependency on an entity manager of type PersistenceContextType.EXTENDED is created, and is said to be bound to the stateful session bean. The dependency on the extended persistence context is declared by means of the PersistenceContext annotation or persistence-context-ref deployment descriptor element. The persistence context is closed by the container when the @Remove method of the stateful session bean completes (or the stateful session bean instance is otherwise destroyed).
Requirements for Persistence Context Propagation
... If a component is called and there is no JTA transaction ..., the persistence context is not propagated. • Invocation of an entity manager defined with PersistenceContext- Type.TRANSACTION will result in use of a new persistence context. • Invocation of an entity manager defined with PersistenceContext- Type.EXTENDED will result in the use of the existing extended persistence context bound to that component.
... If a component is called and the JTA transaction is propagated into that component: • If the component is a stateful session bean to which an extended persistence context has been bound and there is a different persistence context bound to the JTA transaction, an EJBException is thrown by the container. • Otherwise, if there is a persistence context bound to the JTA transaction, that persistence context is propagated and used.
// "Java EE style" declaration of EM
@PersistenceUnit(unitName="H2PU")
EntityManagerFactory emfH2;
EntityManager emH2 = emfH2.createEntityManager();
// "JSE style" declaration of EM
EntityManagerFactory emfH2 = javax.persistence.Persistence.createEntityManagerFactory("H2PU");
EntityManager emH2 = emfH2.createEntityManager();
and the same for emfOracle & emOracle.
// must be run from within Java EE code scope that already has a JTA
// transaction active:
em.joinTransaction();
EntityTransaction tx = em.getTransaction();
tx.begin();
// ....
tx.commit();
@Stateless
public class EmployeeServiceBean implements EmployeeService {
// Transactional method
public void createEmployee() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("EmployeeService");
EntityManager em = emf.createEntityManager();
Employee emp = ...; // set some data
// No need for manual join - em created in active tx context, automatic join:
// em.joinTransaction();
em.persist(emp);
// other data & em operations ...
// call other EJBs to partake in same transaction ...
em.close(); // Note: em can be closed before JTA tx committed.
// Persistence Context will still exist & be propagated
// within JTA tx. Another EM instance could be declared and it
// would propagate & associate the persistence context to it.
// Some time later when tx is committed [at end of this
// method], Data will still be flushed and committed and
// Persistence Context removed .
emf.close();
}
}
@Stateful
public class EmployeeServiceBean implements EmployeeService {
// Because bean is stateful, can store as instance vars and use in multiple methods
private EntityManagerFactory emf;
private EntityManager em;
@PostConstruct // automatically called when EJB constructed and session starts
public void init() {
emf = Persistence.createEntityManagerFactory("EmployeeService");
em = emf.createEntityManager();
}
// Transactional method
public void createEmployee() {
Employee emp = ...; // set some data
em.joinTransaction(); // em created before JTA tx - manual join
em.persist(emp);
}
// Transactional method
public void updateEmployee() {
Employee emp = em.find(...); // load the employee
// don't do join if both methods called in same session - can only call once:
// em.joinTransaction(); // em created before JTA tx - manual join
emp.set(...); // change some data
// no persist call - automatically flushed with commit
}
@Remove // automatically called when EJB session ends
public void cleanup() {
em.close();
emf.close();
}
// ...
}
@TransactionManagement(TransactionManagement.BEAN)
@Stateless
public class EmployeeServiceBean implements EmployeeService {
// inject the JTA transaction interface
@Resource UserTransaction jtaTx;
public void createEmployee() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("EmployeeService");
EntityManager em = emf.createEntityManager();
try {
jtaTx.begin();
try {
em.joinTransaction();
Employee emp = ...; // set some data
em.persist(emp);
// other data & em operations ...
// call other EJBs to partake in same transaction ...
} finally {
jtaTx.commit();
}
} catch (Exception e) {
// handle exceptions from UserTransaction methods
// ...
}
Employee emp = ...; // set some data
// No need for manual join - em created in active tx context, automatic join:
// em.joinTransaction();
em.persist(emp);
em.close(); // Note: em can be closed before JTA tx committed.
// Persistence Context will still exist inside JTA tx.
// Data will still be flushed and committed and Persistence
// Context removed some time later when tx is committed.
emf.close();
}
}
public class ProjectServlet extends HttpServlet {
@EJB ProjectService bean;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// ...
try {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPU");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
bean.assignEmployeeToProject(projectId, empId);
bean.updateProjectStatistics();
} finally {
tx.commit();
}
} catch (Exception e) {
// handle exceptions from EntityTransaction methods
// ...
}
// ...
}
}
public class ProjectServlet extends HttpServlet {
@Resource UserTransaction tx;
@EJB ProjectService bean;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// ...
try {
tx.begin();
try {
bean.assignEmployeeToProject(projectId, empId);
bean.updateProjectStatistics();
EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPU");
EntityManager em = emf.createEntityManager();
// Should be able to avoid explicit call to join transaction.
// Should automatically join because EM created in active tx context.
// em.joinTransaction();
// em operations on data here
em.close();
emf.close();
} finally {
tx.commit();
}
} catch (Exception e) {
// handle exceptions from UserTransaction methods
// ...
}
// ...
}
}
关于java - 为什么具有分离数据源的不同持久化单元查询相同的数据源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15279184/
我试图通过预准备语句使用同一连接执行多个查询,但无法完全实现! 代码片段: public class PostPrReqDaoImpl implements PostPrReqDaoInterface
我目前有一个 2 列宽的 DataGridView,第一列是 DataGridViewTextBoxColumn,第二列是 DataGridViewComboBoxColumn。我还有一个预生成的通用
当我在一台机器上运行以下代码时,我得到了 org.apache.tomcat.dbcp.dbcp.BasicDataSource 的 tomcat 实现,当我在另一台机器上运行它时,我得到了 org.
不确定这是否可行,但这是我的设置。 我有一台带有双启动功能的笔记本电脑。 一个一个分区我有 WinXP 和 MSAccess 2000在另一个分区上,Ubuntu 10.04,带有 apache we
我试过: czmlDataSource.load(czmlurl).then(function(){ viewer.dataSource
我有一个 TableView 和一个数组源。当我在 viewDidLoad 方法中初始化数组时,tableview 显示数组中的数据。当我从 Internet 上的 XML 数据的 URL 填充数组时
我对 DataSource 和 SessionFactory 之间的区别感到困惑。 我认为SessionFactory是一个用于检索 session 的管理器(我猜这实际上是与数据库的连接)。 Dat
我想存储大量(~数千)个字符串并能够使用通配符执行匹配。 例如,这里是一个示例内容: Folder1 文件夹 1/Folder2 Folder1/* Folder1/Folder2/Folder3 文
我有一个 DataGridView 和一个从 SQL 表填充的一些对象的列表。我曾使用两种方法将列表绑定(bind)到网格。 1.直接使用列表到数据源 grdSomeList.DataSource =
我正在尝试在 DataGridView 中设置一些内容。看起来这应该很简单,但我遇到了麻烦。我想显示三列: 代码ID 代号 带有 TypeName 的 DisplayMember 和 TypeID 的
在我的 Config.groovy我把线: grails.config.locations = [ "classpath:app-config.properties"] 我在哪里设置数据源的定义。文件
为了这个问题,假设我有一个包含各种酒类的 Excel 数据源电子表格。 (Cell A) | (Cell B) Bacardi | Rum Smirnoff | Vodka Another Vodka
由于我经常使用第三方 API,我认为创建一些 Magento 模块以实现轻松连接和查询它们会很有帮助。理想情况下,您可以像这样查询 API... $data = Mage::getModel( 'to
将后台线程频繁更新的数据源与 GUI 主线程同步的最佳方法是什么? 我应该在每个方法调用周围放置一个 pthread 互斥体吗?这对我来说似乎也很重。 编辑:我正在寻找 10.5 解决方案 最佳答案
经过几个小时的点击和试用,在查看各种帖子寻求帮助后,这段代码终于起作用了。但我希望有人帮助我理解函数(i,dat),这意味着什么?下面是我的完整代码 - function get_assignedta
我使用的是 Wildfly 10.1 版本,有两个数据源,如下所示, jdbc:mysql://${dbhostn
我正在学习数据源,我想我开始理解它,但我不明白这一段。 据我所知,MySQL 和 PostgreSQL 等数据库供应商编写了自己的不同 DataSource 接口(interface)的实现。现在,这
我有一个关于 TomEE 和使用 tomee.xml 中指定的数据源的奇怪问题。值得注意的是,我使用的是 Netbeans、TomEE 和 MySQL。在 Ubuntu 13.04(Xubuntu 最
WWDC 2019 确实充满了 iOS 的新内容以及 TableViews 和 CollectionView 的新数据源,即 UITableViewDiffableDataSource . 我已成功将
我在独立模式下运行 jboss 并将 standalone.xml 中的数据源设置为以下内容: jdbc:sqlserver://myip:1433;databaseNam
我是一名优秀的程序员,十分优秀!