Harsha
Harsha

Reputation: 3578

Rollback doesn't work in java

I have a method which does a simple mysql insert, when I tried to rollback the insert action as follow, on an error but it is not rollingback on errors, please assist me,

public void addFamer(FamerDTO famer) throws Exception {
        Connection con = JDBCConnectionPool.getInstance().checkOut();
        con.setAutoCommit(false);

        try {


            String generalFamerDataSQL = "INSERT INTO famers(famer_code, name_wt_initials, full_name, gender, "
                    + "nic_or_passport_no, sc_possition, phone_home, phone_mobile, phone_office) VALUES(?,?,?,?,?,?,?,?,?)";
            PreparedStatement insertFamerPS = con.prepareStatement(generalFamerDataSQL, PreparedStatement.RETURN_GENERATED_KEYS);
            insertFamerPS.setString(1, famer.getFamerCode());
            insertFamerPS.setString(2, famer.getNameWithInitials());
            insertFamerPS.setString(3, famer.getNameInFull());
            insertFamerPS.setString(4, famer.getGender());
            insertFamerPS.setString(5, famer.getNICorPassportNo());
            insertFamerPS.setString(6, famer.getSocietyPosission());
            insertFamerPS.setString(7, famer.getHomePhone());
            insertFamerPS.setString(8, famer.getMobilePhone());
            insertFamerPS.setString(9, famer.getOfficePhone());
            insertFamerPS.execute();   


String famerRelations = "INSERT INTO org_reg_blk_soc_fmr(org_id, region_id, famer_id, block_id, soc_id) "
                    + "VALUES (?,?,?,?,?)";
            PreparedStatement famerRelationsPS = con.prepareStatement(famerRelations);
            famerRelationsPS.setInt(1, famer.getOrganization().getOrg_id());
            famerRelationsPS.setInt(2, famer.getRegion().getRegion_id());
            famerRelationsPS.setInt(3, famerID);
            famerRelationsPS.setInt(4, famer.getBlock().getBlockId());
            famerRelationsPS.setInt(6, famer.getSociety().getSoc_id()); //intentionally made an error here to test, put index as 6 for 5
            famerRelationsPS.execute();


            con.commit();
        } catch (Exception e) {
            if (con != null) {
                logger.info("Rolling back!");
                con.rollback();                    
            }
            logger.error(e.getLocalizedMessage());

        } finally {
            con.setAutoCommit(true);
            JDBCConnectionPool.getInstance().checkIn(con);
        }

    }

once this method is called with the required parameters as there is a error in the second insert statement I expected to rollback the first insert action. but thought the error is shown, a record is added to the data base by the first insert statement.

Upvotes: 0

Views: 1114

Answers (1)

Rory Hunter
Rory Hunter

Reputation: 3460

Just to check - what is the table type you're using? Last time I used MySQL, MyISAM tables didn't support transactions, meaning you have to used another table type e.g. InnoDB.

Upvotes: 8

Related Questions