Apache Camel 3.7.3 中的 transacted() 方法可能会影响事务的隔离性。使用 Apache Camel 3.8.0 或更高版本可以解决问题,此外,还可以使用事务管理器提高事务隔离性。以下是使用事务管理器增强隔离性的示例代码:
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
@Component
public class MyRouteBuilder extends RouteBuilder {
private final PlatformTransactionManager transactionManager;
public MyRouteBuilder(PlatformTransactionManager transactionManager){
this.transactionManager = transactionManager;
}
@Override
public void configure() throws Exception {
from("file:/input")
.routeId("input-route")
.transacted("PROPAGATION_REQUIRED")
.to("file:/output")
.end();
}
}
在上面的示例中,我们使用了 transacted("PROPAGATION_REQUIRED")
,表示事务的传播行为为 PROPAGATION_REQUIRED
,即如果当前没有事务,将创建一个新的事务;否则,将使用现有的事务。另外我们注入了事务管理器,使用注入的事务管理器来创建事务。这样可以使事务隔离性更强,确保数据的安全性。