jpa - apache aries 如何恢复失败的事务?
问题描述
我正在使用 Apache karaf 4.2.6 。我有以下配置:
<repository>mvn:org.ops4j.pax.jdbc/pax-jdbc-features/1.3.5/xml/features</repository>
<feature name="karaf-jpa-example-datasource" version="${project.version}">
<config name="org.ops4j.datasource-demo">
osgi.jdbc.driver.class=org.postgresql.Driver
url=jdbc:postgresql://localhost:5432/demo
xa=true
pool=aries
user=postgres
password=postgres
databaseName=demo
dataSourceName=demo
</config>
<capability>
osgi.service;objectClass=javax.sql.DataSource;effective:=active;osgi.jndi.service.name=demo
</capability>
</feature>
<feature name="karaf-jpa-example-common" version="${project.version}">
<feature>transaction</feature>
<feature>jndi</feature>
<feature>pax-jdbc-config</feature>
<feature>pax-jdbc-postgresql</feature>
<feature>pax-jdbc-pool-aries</feature>
<feature>jdbc</feature>
<feature dependency="true">aries-blueprint</feature>
<feature version="[2,3)">jpa</feature>
<feature version="[5,6)">hibernate</feature>
<!--<feature version="[3,4)">openjpa3</feature>-->
<bundle>mvn:org.apache.karaf.examples/karaf-jpa-example-provider-api/${project.version}</bundle>
</feature>
我做了一个简单的方法来进行这样的交易:
@Transactional(Transactional.TxType.REQUIRES_NEW)
@Override
public void add(String flight, String customer) {
int t = 0;
for (;;) {
System.out.println("adding new booking " + t);
Booking booking = new Booking();
booking.setCustomer(customer);
booking.setFlight(flight);
entityManager.persist(booking);
t++;
if (t > 10) {
System.out.println("good bye ");
Runtime.getRuntime().halt(-1);
}
}
}
/etc 下的 apache aries 配置(org.apache.aries.transaction.cfg)是这样的:
aries.transaction.recoverable=true
aries.transaction.timeout=10800
aries.transaction.howl.logFileDir=${karaf.data}/txlogTest
aries.transaction.howl.maxLogFiles=2
aries.transaction.howl.maxBlocksPerFile=512
org.apache.karaf.features.configKey = org.apache.aries.transaction
问题是它没有提交到 txlogTest 日志文件。我想知道 Apache aries 是否支持恢复失败的事务。实际上,我记得当我使用 websphere 时,它启动时会从 tranlog 文件夹中恢复所有失败的事务。我可以用 karaf/Apache Aries 做类似的东西吗?
谢谢
解决方案
您可以在此处查看一个很棒的 Jump Start Karaf JPA 示例
您可以简单地使用@Transactional 注释,如下所示:
@Transactional
public class BookingServiceImpl implements BookingService {
@PersistenceContext(unitName = "booking-hibernate")
private EntityManager entityManager;
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Transactional(Transactional.TxType.REQUIRES_NEW)
@Override
public void add(String flight, String customer) {
Booking booking = new Booking();
booking.setCustomer(customer);
booking.setFlight(flight);
entityManager.persist(booking);
}
}
根据此文档,事务恢复取决于您的资源是否可恢复:
aries.transaction.recoverable属性是启用或不启用可恢复资源支持的标志。可恢复资源是一个事务对象,如果事务提交,其状态将保存到稳定存储中,如果事务回滚,则其状态可以重置为事务开始时的状态。在提交时,事务管理器在与可恢复资源通信时使用两阶段 XA 协议,以确保在提交的事务中涉及多个可恢复资源时的事务完整性。事务数据库和消息代理(如 Apache ActiveMQ)是可恢复资源的示例。可恢复资源使用 JTA 中的 javax.transaction.xa.XAResource 接口表示。默认为真
推荐阅读
- linq - linq external apply with like 语句
- python - 使用多个列表中的值创建数组
- reactjs - TypeError:dispatcher.useState 不是 React Native 的函数
- powershell - 术语“Get-MessageTrackingLog”未被识别为 cmdlet 的名称
- node.js - 内存NodeJS中的.DDS到.PNG转换
- php - Laravel,同一页面上的 2 个表单/ URL 发布到各个路由
- javascript - 为现有表的特定行添加类
- flutter - 有没有办法在最初显示表单时执行 TextFormField 上的 InputFormatters?
- python - 按小时分组,同时考虑夏令时
- c# - 将 texbox 中的数据保存在数组(向量)中