List of usage examples for org.apache.ibatis.session SqlSession rollback
void rollback();
From source file:cern.c2mon.server.history.dao.LoggerDAO.java
License:Open Source License
/** * Inserts into the database a set of rows containing the data coming in * several IFallback objects/* w w w .j a v a 2s .c o m*/ * * @param data * List of IFallback object whose data has to be inserted in the DB * @throws IDBPersistenceException * An exception is thrown in case an error occurs during the data * insertion. The exception provides also the number of already * committed objects */ @SuppressWarnings("unchecked") // add generics to persistence manager public final void storeData(final List data) throws IDBPersistenceException { SqlSession session = null; int size = data.size(); int commited = 0; T tag; try { // We use batch set of statements to improve performance session = sqlSessionFactory.openSession(ExecutorType.BATCH, false); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Obtained batch transacted SQL session (session: " + session.toString() + ")"); } LoggerMapper<T> persistenceMapper = session.getMapper(mapperInterface); // Iterate through the list of DataTagCacheObjects to insert // them one by one for (int i = 0; i != size; i++) { if ((0 == i % RECORDS_PER_BATCH) && i > 0) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("storeData([Collection]) : Commiting rows for i=" + i); } session.commit(); commited = i; } if (data.get(i) != null) { tag = (T) data.get(i); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Logging object with ID: " + tag.getId()); } persistenceMapper.insertLog(tag); } } // Commit the transaction session.commit(); commited = size; } catch (PersistenceException e) { LOGGER.error("storeData([Collection]) : Error executing/closing prepared statement for " + data.size() + " dataTags", e); try { if (session != null) { session.rollback(); } } catch (Exception sql) { LOGGER.error("storeData([Collection]) : Error rolling back transaction.", sql); } throw new IDBPersistenceException(e.getMessage(), commited); } finally { try { if (session != null) { session.close(); } } catch (Exception e) { LOGGER.error("storeData([Collection]) : Error closing session.", e); } } }
From source file:cern.c2mon.server.history.dao.LoggerDAO.java
License:Open Source License
@Override public void storeData(IFallback object) throws IDBPersistenceException { SqlSession session = null; try {//from w w w. j a v a 2s .co m session = sqlSessionFactory.openSession(); LoggerMapper<T> loggerMapper = session.getMapper(mapperInterface); loggerMapper.insertLog((T) object); session.commit(); } catch (PersistenceException ex1) { String message = "Exception caught while persisting an object to the history"; LOGGER.error(message, ex1); if (session != null) session.rollback(); throw new IDBPersistenceException(message, ex1); } finally { if (session != null) session.close(); } }
From source file:com.baomidou.mybatisplus.test.GlobalConfigurationTest.java
License:Apache License
/** * ?/* w ww .j a v a 2 s . c o m*/ */ @SuppressWarnings("unchecked") public static void main(String[] args) { GlobalConfiguration global = GlobalConfiguration.defaults(); global.setAutoSetDbType(true); // FieldStrategy.Empty global.setFieldStrategy(2); BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/mybatis-plus?characterEncoding=UTF-8"); dataSource.setUsername("root"); dataSource.setPassword("521"); dataSource.setMaxTotal(1000); GlobalConfiguration.setMetaData(dataSource, global); // ? InputStream inputStream = GlobalConfigurationTest.class.getClassLoader() .getResourceAsStream("mysql-config.xml"); MybatisSessionFactoryBuilder factoryBuilder = new MybatisSessionFactoryBuilder(); factoryBuilder.setGlobalConfig(global); SqlSessionFactory sessionFactory = factoryBuilder.build(inputStream); SqlSession session = sessionFactory.openSession(false); TestMapper testMapper = session.getMapper(TestMapper.class); /*Wrapper type = Condition.instance().eq("id",1).or().in("type", new Object[]{1, 2, 3, 4, 5, 6}); List list = testMapper.selectList(type); System.out.println(list.toString());*/ Test test = new Test(); test.setCreateTime(new Date()); // ? test.setType(""); testMapper.insert(test); SqlSession sqlSession = sessionFactory.openSession(false); NotPKMapper pkMapper = sqlSession.getMapper(NotPKMapper.class); NotPK notPK = new NotPK(); notPK.setUuid(UUID.randomUUID().toString()); int num = pkMapper.insert(notPK); Assert.assertTrue(num > 0); NotPK notPK1 = pkMapper.selectOne(notPK); Assert.assertNotNull(notPK1); pkMapper.selectPage(RowBounds.DEFAULT, Condition.create().eq("type", 12121212)); NotPK notPK2 = null; try { notPK2 = pkMapper.selectById("1"); } catch (Exception e) { System.out.println(","); } Assert.assertNull(notPK2); int count = pkMapper.selectCount(Condition.EMPTY); Assert.assertTrue(count > 0); int deleteCount = pkMapper.delete(null); Assert.assertTrue(deleteCount > 0); session.rollback(); sqlSession.commit(); }
From source file:com.baomidou.mybatisplus.test.mysql.CircularLabelsTest.java
License:Apache License
/** * // w ww . j a v a2 s.co m */ public static void main(String[] args) { // ? InputStream in = CircularLabelsTest.class.getClassLoader().getResourceAsStream("mysql-config.xml"); MybatisSessionFactoryBuilder mf = new MybatisSessionFactoryBuilder(); SqlSessionFactory sessionFactory = mf.build(in); SqlSession session = sessionFactory.openSession(); UserMapper userMapper = session.getMapper(UserMapper.class); Page<User> page = new Page<>(1, 6); List<User> users = userMapper.forSelect(page, Arrays.asList("1", "2", "3")); System.out.println(users.toString()); System.out.println(page); User user = new User(); user.setId(1L); User users1 = userMapper.selectOne(user); System.out.println(users1); TestMapper mapper = session.getMapper(TestMapper.class); Test test = new Test(); test.setCreateTime(new Date()); test.setType("11111"); mapper.insert(test); session.rollback(); }
From source file:com.baomidou.mybatisplus.test.mysql.TransactionalTest.java
License:Apache License
/** * <p>// ww w. j a va 2s . co 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.bibisco.manager.ArchitectureItemManager.java
License:GNU General Public License
public static void save(ArchitectureItem pArchitectureItem, ArchitectureItemType pArchitectureItemType) { mLog.debug("Start save(ArchitectureItem, ArchitectureItemType)"); SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject(); SqlSession lSqlSession = lSqlSessionFactory.openSession(); try {/*from w w w. j ava 2 s. c om*/ ProjectMapper lProjectMapper = lSqlSession.getMapper(ProjectMapper.class); ProjectWithBLOBs lProjectWithBLOBs = lProjectMapper .selectByPrimaryKey(ContextManager.getInstance().getIdProject()); switch (pArchitectureItemType) { case FABULA: lProjectWithBLOBs.setFabulaTaskStatus(pArchitectureItem.getTaskStatus().getValue()); lProjectWithBLOBs.setFabula(pArchitectureItem.getText()); break; case PREMISE: lProjectWithBLOBs.setPremiseTaskStatus(pArchitectureItem.getTaskStatus().getValue()); lProjectWithBLOBs.setPremise(pArchitectureItem.getText()); break; case SETTING: lProjectWithBLOBs.setSettingTaskStatus(pArchitectureItem.getTaskStatus().getValue()); lProjectWithBLOBs.setSetting(pArchitectureItem.getText()); break; default: break; } lProjectMapper.updateByPrimaryKeyWithBLOBs(lProjectWithBLOBs); lSqlSession.commit(); } catch (Throwable t) { mLog.error(t); lSqlSession.rollback(); throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION); } finally { lSqlSession.close(); } mLog.debug("End save(ArchitectureItem, ArchitectureItemType)"); }
From source file:com.bibisco.manager.ChapterManager.java
License:GNU General Public License
public static ChapterDTO insert(ChapterDTO pChapterDTO) { mLog.debug("Start insert(Chapter)"); SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject(); SqlSession lSqlSession = lSqlSessionFactory.openSession(); try {//from ww w. jav a 2s . co m ChaptersWithBLOBs lChapters = new ChaptersWithBLOBs(); lChapters.setTitle(pChapterDTO.getTitle()); lChapters.setPosition(pChapterDTO.getPosition()); lChapters.setReasonTaskStatus(TaskStatus.TODO.getValue()); ChaptersMapper lChaptersMapper = lSqlSession.getMapper(ChaptersMapper.class); lChaptersMapper.insert(lChapters); pChapterDTO.setIdChapter(lChapters.getIdChapter().intValue()); pChapterDTO.setTaskStatus(TaskStatus.TODO); lSqlSession.commit(); } catch (Throwable t) { mLog.error(t); lSqlSession.rollback(); throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION); } finally { lSqlSession.close(); } mLog.debug("End insert(Chapter)"); return pChapterDTO; }
From source file:com.bibisco.manager.ChapterManager.java
License:GNU General Public License
public static void save(ChapterDTO pChapterDTO) { mLog.debug("Start save(ChapterDTO)"); SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject(); SqlSession lSqlSession = lSqlSessionFactory.openSession(); try {//w w w.j ava 2 s . co m ChaptersWithBLOBs lChapters = new ChaptersWithBLOBs(); lChapters.setIdChapter(pChapterDTO.getIdChapter().longValue()); lChapters.setPosition(pChapterDTO.getPosition()); lChapters.setTitle(pChapterDTO.getTitle()); lChapters.setReasonTaskStatus( pChapterDTO.getReasonTaskStatus() != null ? pChapterDTO.getReasonTaskStatus().getValue() : null); lChapters.setReason(pChapterDTO.getReason()); lChapters.setNote(pChapterDTO.getNote()); ChaptersMapper lChaptersMapper = lSqlSession.getMapper(ChaptersMapper.class); lChaptersMapper.updateByPrimaryKeySelective(lChapters); lSqlSession.commit(); } catch (Throwable t) { mLog.error(t); lSqlSession.rollback(); throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION); } finally { lSqlSession.close(); } mLog.debug("End save(ChapterDTO)"); }
From source file:com.bibisco.manager.ChapterManager.java
License:GNU General Public License
public static void deleteByPosition(Integer pIntIdPosition) { mLog.debug("Start deleteByPosition(Integer)"); SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject(); SqlSession lSqlSession = lSqlSessionFactory.openSession(); try {/*from www .java 2s . c o m*/ // delete scene revisions SceneRevisionsMapper lSceneRevisionsMapper = lSqlSession.getMapper(SceneRevisionsMapper.class); lSceneRevisionsMapper.deleteByChapterPosition(pIntIdPosition); // delete scenes ScenesMapper lScenesMapper = lSqlSession.getMapper(ScenesMapper.class); lScenesMapper.deleteByChapterPosition(pIntIdPosition); // delete chapter ChaptersMapper lChaptersMapper = lSqlSession.getMapper(ChaptersMapper.class); lChaptersMapper.deleteByPosition(pIntIdPosition); // shift down other chapters lChaptersMapper.shiftDown(pIntIdPosition, Integer.MAX_VALUE); lSqlSession.commit(); } catch (Throwable t) { mLog.error(t); lSqlSession.rollback(); throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION); } finally { lSqlSession.close(); } mLog.debug("End deleteByPosition(Integer)"); }
From source file:com.bibisco.manager.ChapterManager.java
License:GNU General Public License
public static void move(Integer pIntSourcePosition, Integer pIntDestPosition) { mLog.debug("Start move(Integer, Integer)"); if (!pIntSourcePosition.equals(pIntDestPosition)) { SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance() .getSqlSessionFactoryProject(); SqlSession lSqlSession = lSqlSessionFactory.openSession(); try {/*from w w w .j a va 2 s. c o m*/ ChaptersExample lChaptersExample = new ChaptersExample(); lChaptersExample.createCriteria().andPositionEqualTo(pIntSourcePosition); ChaptersMapper lChaptersMapper = lSqlSession.getMapper(ChaptersMapper.class); // get chapter to update Chapters lChapters = lChaptersMapper.selectByExample(lChaptersExample).get(0); // update chapter position with fake position to preserve unique index before shift lChapters.setPosition(-1); lChaptersMapper.updateByPrimaryKey(lChapters); // update other chapters' position Integer lIntStartPosition; Integer lIntEndPosition; if (pIntSourcePosition > pIntDestPosition) { lIntStartPosition = pIntDestPosition; lIntEndPosition = pIntSourcePosition; lChaptersMapper.shiftUp(lIntStartPosition, lIntEndPosition); } else { lIntStartPosition = pIntSourcePosition; lIntEndPosition = pIntDestPosition; lChaptersMapper.shiftDown(lIntStartPosition, lIntEndPosition); } // update chapter position lChapters.setPosition(pIntDestPosition); lChaptersMapper.updateByPrimaryKey(lChapters); lSqlSession.commit(); } catch (Throwable t) { mLog.error(t); lSqlSession.rollback(); throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION); } finally { lSqlSession.close(); } } mLog.debug("End move(Integer, Integer)"); }