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.quancheng.mybatis.mapper.test.jdbc.TestJDBC.java

License:Open Source License

public void testJDBC() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {/*from   w  w  w  .jav a 2  s . c om*/
        CountryJDBCMapper mapper = sqlSession.getMapper(CountryJDBCMapper.class);
        CountryJDBC country = new CountryJDBC();
        country.setId(1);
        country.setCountrycode("CN");
        country.setCountryname("China");
        Assert.assertEquals(1, mapper.insert(country));
        Assert.assertNotNull(country.getId());
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.jdbc.TestJDBC.java

License:Open Source License

public void testJDBC2() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {/*from   www. jav  a 2  s.  co  m*/
        CountryJDBCMapper mapper = sqlSession.getMapper(CountryJDBCMapper.class);
        CountryJDBC country = new CountryJDBC();
        country.setId(1);
        country.setCountrycode("CN");
        country.setCountryname("China");
        Assert.assertEquals(1, mapper.insertSelective(country));
        Assert.assertNotNull(country.getId());
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.mysql.TestMysql.java

License:Open Source License

/**
 * ??//from w ww.  j av  a  2  s.c o  m
 */
//?mysqlh2?
//@Test
public void testInsertList() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        List<Country> countryList = new ArrayList<Country>();
        for (int i = 0; i < 10; i++) {
            Country country = new Country();
            country.setCountrycode("CN" + i);
            country.setCountryname("?" + i);
            countryList.add(country);
        }
        int count = mapper.insertList(countryList);
        Assert.assertEquals(10, count);
        for (Country country : countryList) {
            Assert.assertNotNull(country.getId());
        }
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.mysql.TestMysql.java

License:Open Source License

/**
 * ??/*from ww w .j a  v  a2  s.  c o m*/
 */
//?mysqlh2?
//@Test
public void testInsert() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Country country = new Country();
        country.setCountrycode("CN");
        country.setCountryname("?");
        int count = mapper.insertUseGeneratedKeys(country);
        Assert.assertEquals(1, count);
        Assert.assertNotNull(country.getId());
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.user.TestBasic.java

License:Open Source License

/**
 * //from   w w w. j a va 2 s . c  o  m
 */
@Test
public void testInsert() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("abel533");
        userInfo.setPassword("123456");
        userInfo.setUsertype("2");
        userInfo.setEmail("abel533@gmail.com");
        Collection collection = sqlSession.getConfiguration().getMappedStatements();
        for (Object o : collection) {
            if (o instanceof MappedStatement) {
                MappedStatement ms = (MappedStatement) o;
                if (ms.getId().contains("UserInfoMapper.insert")) {
                    System.out.println(ms.getId());
                }
            }
        }

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

        Assert.assertNotNull(userInfo.getId());
        Assert.assertTrue((int) userInfo.getId() >= 6);

        Assert.assertEquals(1, mapper.deleteByPrimaryKey(userInfo));
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.user.TestBasic.java

License:Open Source License

/**
 * ?/*from w  w  w . ja va 2s  . c  om*/
 */
@Test
public void testDelete() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
        //
        Assert.assertEquals(5, mapper.selectCount(new UserInfo()));
        //100
        UserInfo userInfo = mapper.selectByPrimaryKey(1);

        //?
        Assert.assertEquals(1, mapper.deleteByPrimaryKey(1));

        //
        Assert.assertEquals(4, mapper.selectCount(new UserInfo()));
        //?
        Assert.assertEquals(1, mapper.insert(userInfo));
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}

From source file:com.quancheng.mybatis.mapper.test.user.TestBasic.java

License:Open Source License

/**
 * ?//  w  w w . j av  a2  s  .  co  m
 */
@Test
public void testUpdateByPrimaryKey() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
        UserInfo userInfo = mapper.selectByPrimaryKey(2);
        Assert.assertNotNull(userInfo);
        userInfo.setUsertype(null);
        userInfo.setEmail("abel533@gmail.com");
        //?username
        Assert.assertEquals(1, mapper.updateByPrimaryKey(userInfo));

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

From source file:com.quancheng.mybatis.mapper.test.user.TestBasic.java

License:Open Source License

/**
 * ??null/* w w w .  j a  v  a 2s  . c  o  m*/
 */
@Test
public void testUpdateByPrimaryKeySelective() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
        UserInfo userInfo = mapper.selectByPrimaryKey(1);
        Assert.assertNotNull(userInfo);
        userInfo.setUsertype(null);
        userInfo.setEmail("abel533@gmail.com");
        //?username
        Assert.assertEquals(1, mapper.updateByPrimaryKeySelective(userInfo));

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

From source file:com.skt.adcas.lte.action.CSVDownloadAction.java

public String selectBasicData() {

    this.log.debug("selectBasicData Start");
    SqlSession session = null;
    try {/*w w w. j  av  a2 s.c  o  m*/
        parseParam();
        session = SqlSessionManager.getSqlSession().openSession();

        String writeFolderPath = (String) super.properties.get("TEMP_FOLDER_PATH");
        String tempFolder = "/" + UUID.randomUUID().toString();
        String xlsFileName = "/nice.csv";
        String xlsFileFullPath = writeFolderPath + tempFolder + xlsFileName;
        log.debug(xlsFileFullPath);

        if (!(new File(writeFolderPath + tempFolder)).mkdir()) {
            throw new Exception("? ??  .");
        }

        //this.fileOut = new FileOutputStream(xlsFileFullPath);

        FileOutputStream fos = new FileOutputStream(xlsFileFullPath);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "EUC-KR");
        this.fileOut = new BufferedWriter(osw);
        //this.fileOut = new BufferedWriter(new FileWriter(xlsFileFullPath));

        writeHeader();

        log.debug(param);

        session.select("BigDownload.selectBasicData_" + param.get("SEARCHTYPE"), param, this);

        fileOut.close();

        this.msg = "? ? ?";
        this.status = "SUCCESS";
        this.downloadurl = /*"download" + */ tempFolder + xlsFileName;

        session.commit();
        this.msg = "??";
        this.status = "SUCCESS";
    } catch (Exception e) {
        e.printStackTrace();
        this.msg = e.getMessage();
        this.status = "ERROR";
        this.error = true;
        session.rollback();

    } finally {
        session.close();
    }
    return SUCCESS;
}

From source file:com.skt.adcas.lte.action.DownLinkByNMSAction.java

public String selectDailyCellTraffic() {

    this.log.debug("selectDailyCellTraffic Start");
    SqlSession session = null;

    try {//from  w w w .  jav a2s .  c o  m
        parseParam();

        /*            if( isNull(this.BTS_NM_CMS).equals("") ) {
        throw new Exception("DU? ");
                    }
        */
        this.log.debug("######################   ");
        session = SqlSessionManager.getSqlSession().openSession();

        //this.param.put("BTS_NM_CMS",BTS_NM_CMS);
        this.log.debug("######################  ??  ");
        this.rows = session.selectList("DownLinkByNMS.selectDailyCellTraffic" + VIEWTYPE, param);

        this.log.debug("###################### ");
        //session.commit();
        this.log.debug("###################### ");
        this.msg = "?";
        this.status = "SUCCESS";
    } catch (Exception e) {
        this.msg = e.getMessage();
        this.status = "ERROR";
        this.error = true;
        if (session != null) {
            session.rollback();
        }
        e.printStackTrace();
    } finally {
        if (session != null) {
            session.close();
        }
    }

    this.log.debug("selectDailyCellTraffic End");
    return SUCCESS;
}