Example usage for org.apache.ibatis.session SqlSession rollback

List of usage examples for org.apache.ibatis.session SqlSession rollback

Introduction

In this page you can find the example usage for org.apache.ibatis.session SqlSession rollback.

Prototype

void rollback();

Source Link

Document

Discards pending batch statements and rolls database connection back.

Usage

From source file:com.github.abel533.entity.test.TestMBGExampleNull.java

License:Open Source License

@Test
public void testUpdateByExampleSelective() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {//from   www  .j a v a2 s. com
        CommonMapper commonMapper = sqlSession.getMapper(CommonMapper.class);
        EntityMapper entityMapper = new EntityMapper(commonMapper);

        Country country = new Country();
        country.setCountryname("?");
        int count = entityMapper.updateByExampleSelective(country, null);

        Assert.assertEquals(183, count);
    } finally {
        //
        sqlSession.rollback();
        sqlSession.close();
    }
}

From source file:com.github.abel533.entity.test.TestMBGExampleNull.java

License:Open Source License

@Test
public void testUpdateByExample() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {//from  w  ww .  jav  a  2  s. co  m
        CommonMapper commonMapper = sqlSession.getMapper(CommonMapper.class);
        EntityMapper entityMapper = new EntityMapper(commonMapper);

        Country country = new Country();
        country.setCountryname("");
        int count = entityMapper.updateByExample(country, null);

        Assert.assertEquals(183, count);
    } finally {
        //
        sqlSession.rollback();
        sqlSession.close();
    }
}

From source file:com.github.abel533.entity.test.TestUpdateByPrimaryKey.java

License:Open Source License

@Test
public void testUpdateByPrimaryKey() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {/*w w w .  ja  va2 s  .  c o m*/
        CommonMapper commonMapper = sqlSession.getMapper(CommonMapper.class);
        EntityMapper entityMapper = new EntityMapper(commonMapper);
        Country country = new Country();
        country.setId(100);
        country.setCountryname("");
        int count = entityMapper.updateByPrimaryKey(country);
        Assert.assertEquals(1, count);

        country = entityMapper.selectByPrimaryKey(Country.class, 100);
        Assert.assertNull(country.getCountrycode());
        Assert.assertEquals("", country.getCountryname());
    } finally {
        //
        sqlSession.rollback();
        sqlSession.close();
    }
}

From source file:com.github.abel533.entity.test.TestUpdateByPrimaryKeySelective.java

License:Open Source License

@Test
public void testUpdateByPrimaryKeySelective() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {/*  w  w  w .  j  a v  a 2s .c o  m*/
        CommonMapper commonMapper = sqlSession.getMapper(CommonMapper.class);
        EntityMapper entityMapper = new EntityMapper(commonMapper);
        Country country = new Country();
        country.setId(100);
        country.setCountryname("");
        int count = entityMapper.updateByPrimaryKeySelective(country);
        Assert.assertEquals(1, count);

        country = entityMapper.selectByPrimaryKey(Country.class, 100);
        Assert.assertNotNull(country.getCountrycode());
        Assert.assertEquals("", country.getCountryname());
    } finally {
        //
        sqlSession.rollback();
        sqlSession.close();
    }
}

From source file:com.glaf.core.test.MyBatisBatchTest.java

License:Apache License

@Test
public void testInsert() {
    SqlSessionFactory sqlSessionFactory = super.getBean("sqlSessionFactory");
    SqlSession sqlSession = null;
    try {//from   w  w w  . ja v  a  2s  .  co m
        sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        long id = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            SysLog log = new SysLog();
            log.setId(id + i);
            log.setAccount("test");
            log.setCreateTime(new Date());
            log.setIp("192.168.0.12");
            log.setOperate("insert");
            log.setContent("Test Insert");
            sqlSession.insert("insertSysLog", log);
            if (i == 9999) {
                // throw new RuntimeException("throw exception");
            }
        }
        sqlSession.commit();
    } catch (Exception ex) {
        if (sqlSession != null) {
            sqlSession.rollback();
        }
        ex.printStackTrace();
        throw new RuntimeException(ex);
    } finally {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }
}

From source file:com.glaf.core.test.MyBatisBatchTest2.java

License:Apache License

@Test
public void testInsert() {
    SqlSessionFactory sqlSessionFactory = super.getBean("sqlSessionFactory");
    SqlSession sqlSession = null;
    try {/*  w  w w. j  av a2s .c o  m*/
        Environment.setCurrentSystemName("wechat");
        HibernateBeanFactory.reload();
        sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        long id = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            SysLog log = new SysLog();
            log.setId(id + i);
            log.setAccount("test");
            log.setCreateTime(new Date());
            log.setIp("192.168.0.12");
            log.setOperate("insert");
            log.setContent("Test Insert");
            sqlSession.insert("insertSysLog", log);
            if (i == 999) {
                // throw new RuntimeException("throw exception");
            }
        }
        sqlSession.commit();
    } catch (Exception ex) {
        if (sqlSession != null) {
            sqlSession.rollback();
        }
        ex.printStackTrace();
        throw new RuntimeException(ex);
    } finally {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }
}

From source file:com.mybatisX.test.mysql.TransactionalTest.java

License:Apache License

/**
 * <p>//from www.ja  va 2  s.c  o  m
 * 
 * </p>
 */
public static void main(String[] args) {
    /*
     * ?
     */
    InputStream in = TransactionalTest.class.getClassLoader().getResourceAsStream("mysql-config.xml");
    MybatisSessionFactoryBuilder mf = new MybatisSessionFactoryBuilder();
    SqlSessionFactory sessionFactory = mf.build(in);
    SqlSession sqlSession = sessionFactory.openSession();
    /**
     * ?
     */
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    int rlt = userMapper.insert(new User(IdWorker.getId(), "1", 1, 1));
    System.err.println("--------- insertInjector --------- " + rlt);

    //session.commit();
    sqlSession.rollback();
    sqlSession.close();
}

From source file:com.quancheng.mybatis.mapper.test.able.TestBasicAble.java

License:Open Source License

/**
 * /*w  w w .  ja v a 2 s  .  c om*/
 */
@Test
public void testInsert() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        UserInfoAbleMapper mapper = sqlSession.getMapper(UserInfoAbleMapper.class);
        UserInfoAble userInfo = new UserInfoAble();
        userInfo.setUsername("abel533");
        userInfo.setPassword("123456");
        userInfo.setUsertype("2");
        userInfo.setEmail("abel533@gmail.com");//insert=false

        Assert.assertEquals(1, mapper.insert(userInfo));

        Assert.assertNotNull(userInfo.getId());
        Assert.assertEquals(6, (int) userInfo.getId());

        userInfo = mapper.selectByPrimaryKey(userInfo.getId());
        //email?
        Assert.assertNull(userInfo.getEmail());
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.able.TestBasicAble.java

License:Open Source License

/**
 * ?//  w  w w.j  a va  2  s .c o  m
 */
@Test
public void testUpdateByPrimaryKey() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        UserInfoAbleMapper mapper = sqlSession.getMapper(UserInfoAbleMapper.class);
        UserInfoAble userInfo = mapper.selectByPrimaryKey(2);
        Assert.assertNotNull(userInfo);
        userInfo.setUsertype(null);
        userInfo.setEmail("abel533@gmail.com");
        userInfo.setAddress("??");//update=false
        //?username
        Assert.assertEquals(1, mapper.updateByPrimaryKey(userInfo));

        userInfo = mapper.selectByPrimaryKey(userInfo);
        Assert.assertNull(userInfo.getUsertype());
        Assert.assertNotEquals("??", userInfo.getAddress());
        Assert.assertEquals("abel533@gmail.com", userInfo.getEmail());
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.able.TestBasicAble.java

License:Open Source License

/**
 * ??null/*from   w  w  w. j a v  a  2s.com*/
 */
@Test
public void testUpdateByPrimaryKeySelective() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        UserInfoAbleMapper mapper = sqlSession.getMapper(UserInfoAbleMapper.class);
        UserInfoAble userInfo = mapper.selectByPrimaryKey(1);
        Assert.assertNotNull(userInfo);
        userInfo.setUsertype(null);
        userInfo.setPassword(null);
        userInfo.setAddress("??");
        //?username
        Assert.assertEquals(1, mapper.updateByPrimaryKeySelective(userInfo));

        userInfo = mapper.selectByPrimaryKey(1);
        Assert.assertEquals("1", userInfo.getUsertype());
        Assert.assertEquals("12345678", userInfo.getPassword());
        Assert.assertNotEquals("??", userInfo.getAddress());
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}