我试图了解使远程/网络资源符合 JTA 的基本原则,但我对有关该主题的文档/博客/文章如此之少感到震惊。
假设我编写了自己的特殊类型的服务器,即“ IAmYourFaja ”服务器或“ IAYF ”。假设我编写/实现了我自己的基于 TCP 的网络协议,用于与该服务器进行交互,称为 IAYFCP(IAYF 通信协议)。最后,我编写了一个 Java 客户端库,用于通过 IAYFCP 访问和发送消息到远程 IAYF 服务器。还在我这儿?
现在我有一个用例,我需要执行以下分布式事务:
我需要所有这些进行交易,这样如果任何一个组件在任何时候出现故障,我都可以将它们全部回滚,并且这些网络资源中的状态不会发生任何改变。
最终目标是能够运行以下代码(此处为伪代码):
// "JTA Example"
DistributedTransaction dTrans = getTransaction();
DataSource jdbcDataSource = getDataSource();
IayfClient iayfClient = getIayfClient();
JmsClient jmsClient = getJmsClient();
try {
dTrans.begin();
// 1. Insert a record in a relational/JDBC database
insertRecord(jdbcDataSource, "INSERT INTO widgets ( fizz, buzz ) VALUES ( 35, true )");
// 2. Fire a message …Run Code Online (Sandbox Code Playgroud) 根据Hibernate文档,在JTA环境中,默认的连接释放模式是after_statement,这意味着在每个语句之后释放hibernate逻辑连接.
释放逻辑连接后,将调用Connection close()方法,并从事务管理器中取消列出当前资源.
根据RedHat 交易开发者指南:
"delistResource方法用于将指定资源与目标对象中的事务上下文分离.应用程序服务器使用两个参数调用该方法:
An XAResources object, which represents the resource.
A flag to indicate whether the operation is due to the transaction being suspended (TMSUSPEND), a portion of the work has failed (TMFAIL), or a normal resource release by the application (TMSUCCESS)."
Run Code Online (Sandbox Code Playgroud)
由于Bitronix使用TMSUCCESS:
currentTransaction.delistResource(xaResourceHolderState.getXAResource(), XAResource.TMSUCCESS);
Run Code Online (Sandbox Code Playgroud)
这意味着连接与当前事务分支取消关联,有时您可能最终为同一资源适配器争用2个不同的连接.
我认为保持连接的次数与事务一样多是一个更好的选择,因为我们通常每个事务执行多个语句.所以after_transaction释放模式听起来更有吸引力.
使用Bitronix,after_transaction释放模式更合适吗?有没有人在生产环境中体验过它?
嗨,我有一个使用spring和hibernate的Java Web应用程序.
我有一个名为Role的模型类.对于所有模型类,都有常见的GenericDao.java
public interface GenericDao<T>{
public void insert(T transientInstance);
public T findById(Class<?> clazz, Serializable id);
}
Run Code Online (Sandbox Code Playgroud)
GenericHibernateDao.java
@Repository
public class GenericHibernateDao<T extends Serializable>
implements GenericDao<T>{
@Resource
protected SessionFactory sessionFactory;
@Override
public void insert(T transientInstance) {
sessionFactory.getCurrentSession().persist(transientInstance);
}
@SuppressWarnings("unchecked")
@Override
public T findById(Class<?> clazz, Serializable id) {
return (T) sessionFactory.getCurrentSession().get(clazz, id);
}
}
Run Code Online (Sandbox Code Playgroud)
RoleService.java
public interface RoleService {
public void insert(Role role);
public Role findById(Integer id);
}
Run Code Online (Sandbox Code Playgroud)
并且有它的实现RoleServiceImpl.java
@Service
public class RoleServiceImpl implements RoleService {
@Autowired …Run Code Online (Sandbox Code Playgroud) 我试图环绕我的头值在Java事务API(JTA)及其实现,Bitronix之一的下方.但随着我对文档的深入挖掘,我不禁想到以下简单的例子:
public interface Transactional {
public void commit(Object);
public void rollback();
}
public class TransactionalFileWriter extends FileWriter implements Transactional {
@Override
public void commit(Object obj) {
String str = (String)obj;
// Write the String to a file.
write(str);
}
@Override
public void rollback() {
// Obtain a handler to the File we are writing to, and delete the file.
// This returns the file system to the state it was in before we created a file and started …Run Code Online (Sandbox Code Playgroud) 我创建了一个 Spring Boot 2 (2.1.6.RELEASE) 项目,该项目依赖于 spring-boot-starter-data-jpa 和 spring spring-boot-starter-jta-bitronix,并为 Mysql DB (8.0.16 )。
应用程序属性文件(根据 <> 之间的占位符值的相关性进行修剪)包含以下配置:
spring:
datasource:
url: jdbc:mysql://<host>:<port>/<dbName>
username: <username>
password: <password>
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
database-platform: org.hibernate.dialect.MySQL8Dialect
hibernate:
ddl-auto: none
jta:
bitronix:
properties:
server-id: <serverid>
Run Code Online (Sandbox Code Playgroud)
在启动 Spring Boot 应用程序时,我收到以下错误堆栈跟踪:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactory' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactoryBuilder' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied …Run Code Online (Sandbox Code Playgroud) 在Moqui中,我尝试配置为使用mysql,在defaultconf中注释掉了derby和未注释的mysql,我将连接器复制到了框架lib,在框架build.gradle中包含了依赖项,在运行负载时,出现此错误-java.lang .reflect.InvocationTargetExceptionjavax.management.InstanceAlreadyExistsException:bitronix.tm:type=JDBC,UniqueName=DEFAULT_transactional_DS,Id=0-感谢您的帮助