[英]How to set up Spring Boot + Bitronix + non-XA Datasource + XA JMS Connection
[英]How to configure Oracle XA Datasource on spring boot app that communicating with an EJB in JBoss EAP
我正在嘗試在 Spring 啟動應用程序上使用 EJB。 此應用程序在“undertow”中運行,它可以在 JBoss EAP (Jboss EAP 7.0) 上調用 EJB。
我嘗試將應用程序配置為使用 Oracle XA 事務但沒有成功。
我需要的是,當 Spring 啟動應用程序調用 EJB 並且在調用之后,此應用程序發生一些故障,即 EJB 回滾以前的工作。
遵循 spring 引導應用程序的一些配置:
絨球
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-ejb-client-bom</artifactId>
<version>17.0.0.Final</version>
<type>pom</type>
</dependency>
</dependencies>
特性
spring.datasource.url=jdbc:oracle:thin:@<host>:<port>:<data>
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.xa.data-source-class-name=bitronix.tm.resource.jdbc.lrc.LrcXADataSource
spring.datasource.xa.properties.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.jpa.properties.hibernate.hbm2ddl.schema_filter_provider=<class>
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.integration.envers.enabled=false
應用程序 Class
Configuration
@SpringBootApplication
@EnableTransactionManagement
@ComponentScan(basePackages={...,...})
@EntityScan(basePackages={...,...,...})
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication .class, args);
}
EJB“生產者”
@Configuration
public class EJBConfiguration {
@Bean
public Context context() throws NamingException {
Properties jndiProps = new Properties();
jndiProps.put("java.naming.factory.initial", "org.jboss.naming.remote.client.InitialContextFactory");
jndiProps.put("jboss.naming.client.ejb.context", true);
jndiProps.put("java.naming.provider.url", "http-remoting://localhost:8081");
return new InitialContext(jndiProps);
}
@Bean
public EJBRemote(Context c) throws NamingException {
return (EJBRemote) c.lookup("full EJB remote name")
}
}
EJB的調用如下: stuff
Spring 啟動應用程序 controller
class AppControler {
@AutoWired
Service service
@PostMapping("/")
@Transactional
public ResponseEntity controllerMethod() {
return new ResponseEntity<>(this.service.doStuff(), HttpStatus.OK);
}
}
Spring 啟動應用服務
class Service {
@Autowired
private EJBRemote EJBRemote;
@Autowired
private DAOObject dao;
@TransactionAttribute(TransactionAttributeType.MANDATORY)
Public Object doStuff() {
dao.save();
EJBRemote.saveSomeThing(); // OK operation, needs to rolback if oerros occurs after
dao.saveOtherThing();// errors occurs
}
}
JBoss EAP 上的 EJB 實現 class 就像
class EJBRemoteImpl implements EJBRemote {
@Override
public void saveSomeThing() {
someDao.save();
}
}
所以當 'dao.saveOtherThing();' 發生錯誤時 'EJBRemove.saveSomeThing();' 中的操作需要回滾。
如果我在 EJB 上放置“@TransactionAttribute(TransactionAttributeType.MANDATORY)”,則會發生此錯誤:
javax.ejb.EJBTransactionRequiredException: WFLYEJB0062: A transaction is required to call org.jboss.invocation.InterceptorContext@493c9dbb
at org.jboss.as.ejb3.tx.CMTTxInterceptor.mandatory(CMTTxInterceptor.java:289) ~[na:na]
at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:233) ~[na:na]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) ~[na:na]
at org.jboss.as.ejb3.remote.EJBRemoteTransactionPropagatingInterceptor.processInvocation(EJBRemoteTransactionPropagatingInterceptor.java:79) ~[na:na]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) ~[na:na]
at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41) ~[na:na]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) ~[na:na]
at org.jboss.as.ejb3.component.invocationmetrics.WaitTimeInterceptor.processInvocation(WaitTimeInterceptor.java:43) ~[na:na]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) ~[na:na]
.
.
.
如果注釋 (@TransactionAttribute(TransactionAttributeType.MANDATORY)) 不存在,則對 EJB 的調用正常發生,但不會發生回滾。
您正在嘗試在 Spring 服務中創建 EJB 事務,如果您想這樣做,您需要將您的服務轉換為 EJB,因此所有操作都會自動為TransactionAttribute.REQUIRED
,如果您希望此方法與 Spring 事務一起使用,您需要使用 Spring ( org.springframework.transaction.annotation.Transactional
) 中的@Transactional(propagation = Propagation.MANDATORY)
,但我不知道 Spring 事務是否適用於分布式上下文,但您可以嘗試。 如果不將 Bean 轉換為 EJB 將起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.