Question

i have a project that uses myBatis for persistence. method "A" below was working just fine until I added some foreign keys and converted my table from myISAM to innoDB. After the conversion, method "A" would fail silently, not even a warning in the logs. After the conversion, only method "B" does a successful insert. Both methods write the correct sql to the logs, but only "B" works.

Can anyone fill me in on why i need to do a commit now, but did not have to do a commit previously?

//doesnt work, but worked previously
public void A(Role role) {
    SqlSession session = sqlSessionFactory.openSession();
    try {
        RoleMapper mapper = session.getMapper(RoleMapper.class);
        mapper.updateByPrimaryKeySelective(role);
    }catch(Exception e){
        logger.error(e);
    } finally {
        session.close();
    }
    return;
}

//works correctly, but why?
public void B(Role role) {
    SqlSession session = sqlSessionFactory.openSession();
    try {
        RoleMapper mapper = session.getMapper(RoleMapper.class);
        mapper.updateByPrimaryKeySelective(role);
        session.commit();
    }catch(Exception e){
        logger.error(e);
    } finally {
        session.close();
    }
    return;
}
Was it helpful?

Solution

myISAM is not transactional. Autocommit is on by default (actually it's ignored by the JDBC driver since every statement commits). innoDB is transactional and autocommit is also off by default. This means you have to call session.commit() or the DB never actually does the update.

See this blog entry for more information.

Note that you should call commit rather than leave things up to autocommit. Leaving autocommit off will cause problems with connection pooling since it can leave statements in an unknown state when the connection is reused.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top