Невозможно выполнить откат транзакции пользователя BMT EJB в MYSQL - PullRequest
0 голосов
/ 11 января 2020

Мое приложение использует Struts2, EJB3-сеанс без сохранения состояния, JDB C, MYSQL. Я не могу откатить завершенную транзакцию для исключения. Я использовал BMT, пользовательскую транзакцию в EJB3 Statemless bean-компоненте и использовал JDB C, а база данных - MYSQL. Я вставляю три записи, одну в родительскую таблицу, две записи в дочернюю. Я высмеиваю исключение после вставки в Родителя и одного Дочернего объекта, пользовательская транзакция может откатиться от дочернего элемента, но не от родительского элемента. Пожалуйста, сообщите

Пожалуйста, найдите код ниже EJB

@Stateless
@Remote
@RemoteBinding(jndiBinding = AppConstants.JNDI_BINDING_CTRADE)
@TransactionManagement(value=TransactionManagementType.BEAN)
public class CManager implements CManagement {


        @Override
        public void saveDetails(String sessionId) {
            try {
                // TODO Auto-generated method stub
                 userTransaction.begin();
                CInterface cInterface = createCInstance(sessionId, true);
                cInterface.saveDetails3(sessionId);
                cInterface.saveDetails2(sessionId);
                String st=null;
                if(st.equals("test")); // So fake a null pointer
                cInterface.saveDetails1(sessionId);             
                 userTransaction.commit();
            } catch (Exception e) {
                 try {
                    userTransaction.rollback();
                    e.printStackTrace();
                } catch (IllegalStateException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (SecurityException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (SystemException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }




}

DAO

    @Override
    public void saveDetails3(String sessionId) throws Exception {
        // TODO Auto-generated method stub
        PreparedStatement pstmt = null;
        PreparedStatement pstm2 = null;
        PreparedStatement pstm3 = null;

        Connection connection = null;
        Integer id =0, id2 =0,id3 =0;
        String query1= "Insert into transaction(transactionId,outputProductId) values ('ST8' , '108')";

        try {
            connection = connectionManager.getConnection();
            pstmt = connection.prepareStatement(query1,
                    Statement.RETURN_GENERATED_KEYS);
            pstmt.execute();
            ResultSet rs = pstmt.getGeneratedKeys();
            while (rs.next()) {
                id = rs.getInt(1);
            }
            System.out.println("ST id" + id);



        } catch (SQLException se) {
            LOGGER.error(se.getMessage(), se);
            throw se;
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            throw e;
        } finally {
            /* Closing Connection Objects */
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    LOGGER.error(e.getMessage(), e);
                    throw e;
                }
            }
        }

    }

@Override
    public void saveDetails1(String sessionId) throws Exception {
        // TODO Auto-generated method stub
        PreparedStatement pstmt = null;
        PreparedStatement pstm2 = null;
        PreparedStatement pstm3 = null;

        Connection connection = null;
        Integer id =0, id2 =0,id3 =0;
        String query1= "Insert into child(transactionId , inputproductId ,outputproductId ,ratio) values ('ST8' , 'I08' ,'O01','5');";

        try {
            connection = connectionManager.getConnection();
            pstmt = connection.prepareStatement(query1,
                    Statement.RETURN_GENERATED_KEYS);
            pstmt.execute();
            ResultSet rs = pstmt.getGeneratedKeys();
            while (rs.next()) {
                id = rs.getInt(1);
            }
            System.out.println(" id1" + id);



        } catch (SQLException se) {
            LOGGER.error(se.getMessage(), se);
            throw se;
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            throw e;
        } finally {
            /* Closing Connection Objects */
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    LOGGER.error(e.getMessage(), e);
                    throw e;
                }
            }
        }       
    }

    @Override
    public void saveDetails2(String sessionId) throws Exception {
        // TODO Auto-generated method stub
        PreparedStatement pstmt = null;
        PreparedStatement pstm2 = null;
        PreparedStatement pstm3 = null;

        Connection connection = null;
        Integer id =0, id2 =0,id3 =0;
        String query1= "Insert into child(transactionId , inputproductId ,outputproductId ,ratio) values ('ST8' , 'I07' ,'O02','5');";

        try {
            connection = connectionManager.getConnection();
            pstmt = connection.prepareStatement(query1,
                    Statement.RETURN_GENERATED_KEYS);
            pstmt.execute();
            ResultSet rs = pstmt.getGeneratedKeys();
            while (rs.next()) {
                id = rs.getInt(1);
            }
            System.out.println(" id2 " + id);



        } catch (SQLException se) {
            LOGGER.error(se.getMessage(), se);
            throw se;
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            throw e;
        } finally {
            /* Closing Connection Objects */
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    LOGGER.error(e.getMessage(), e);
                    throw e;
                }
            }
        }       
    }

Здесь транзакция частично отменена

...