Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JBoss数据源故障切换如何处理单个事务中的故障?_Java_Jboss_Entitymanager_Failover - Fatal编程技术网

Java JBoss数据源故障切换如何处理单个事务中的故障?

Java JBoss数据源故障切换如何处理单个事务中的故障?,java,jboss,entitymanager,failover,Java,Jboss,Entitymanager,Failover,这是我在JBossstandalone.xml中的连接详细信息 <connection-url> jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=xx.1xx.119.1xx)(PORT=1521))(LOAD_BALANCE=on)(FAILOVER=on))(CONNECT

这是我在JBoss
standalone.xml中的连接详细信息

<connection-url>
    jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=xx.1xx.119.1xx)(PORT=1521))(LOAD_BALANCE=on)(FAILOVER=on))(CONNECT_DATA=(SERVICE_NAME=XE)))
</connection-url>
但这一点也无济于事。捕获异常后,使用JNDI查找获取新bean不包含更新的新连接,并引发异常。这将导致该事务的数据丢失


请建议如何处理“获取EntityManager后和坚持之前的连接丢失”这一极端情况。

我认为您想要实现的目标是不可能的。问题是,如果内部DB事务被中止,那么JTA事务将处于中止状态,您无法继续执行它

我想这和这个案子差不多

 @Stateless
 public class TableCreator {
    @Resource
    DataSource datasource;

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public void create() {
        try(Connection connection = datasource.getConnection()) {
            Statement st = connection.createStatement();
            st.execute("CREATE TABLE user (id INTEGER NOT NULL, name VARCHAR(255))");
        } catch (SQLException sqle) {
           // ignore this as table already exists
        }
    }
 }

 @Stateless
 public class Inserter {
   @EJB
   private TableCreator creator;

    public void call() {
        creator.create();

        UserEntity entity = new UserEntity(1, "EAP QE");
        em.persist(entity);
    }
 }
如果存在表
user
,并且您将使用注释
@TransactionAttribute(TransactionAttributeType.REQUIRED)
,则
创建
调用将是与
持久化调用相同的jta全局事务的一部分。在这种情况下,事务被中止,
persist
调用将失败,出现异常,如(postgresql案例)

我的意思是,如果Oracle jdbc驱动程序无法透明地处理JBoss app server的连接失败,并向上抛出异常,那么我认为唯一可能的解决方案是重复整个更新操作

 @Stateless
 public class TableCreator {
    @Resource
    DataSource datasource;

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public void create() {
        try(Connection connection = datasource.getConnection()) {
            Statement st = connection.createStatement();
            st.execute("CREATE TABLE user (id INTEGER NOT NULL, name VARCHAR(255))");
        } catch (SQLException sqle) {
           // ignore this as table already exists
        }
    }
 }

 @Stateless
 public class Inserter {
   @EJB
   private TableCreator creator;

    public void call() {
        creator.create();

        UserEntity entity = new UserEntity(1, "EAP QE");
        em.persist(entity);
    }
 }
Caused by: org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block