List of usage examples for org.apache.ibatis.session SqlSession close
@Override
void 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 {/* w w w . j a v a 2s . c om*/ 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 {/*from w w w . j av a2 s . 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.glaf.core.test.MyBatisMultiConnectionTest.java
License:Apache License
public static void main(String[] args) { long start = System.currentTimeMillis(); SqlSession session = null; Connection conn = null;//ww w .j a v a 2 s . c o m try { conn = DBConnectionFactory.getConnection(); session = sqlSessionFactory.openSession(conn); User user = session.selectOne("getUserById", "root"); System.out.println(user.toJsonObject().toJSONString()); } catch (Exception ex) { ex.printStackTrace(); } finally { if (session != null) { session.close(); } JdbcUtils.close(conn); } long time = System.currentTimeMillis() - start; System.out.println("" + (time)); for (int i = 0; i < 100; i++) { start = System.currentTimeMillis(); session = null; conn = null; try { conn = DBConnectionFactory.getConnection("yz"); session = sqlSessionFactory.openSession(conn); TreeModel tree = session.selectOne("getTreeModelByCode", "SYS000"); System.out.println(tree.toJsonObject().toJSONString()); } catch (Exception ex) { ex.printStackTrace(); } finally { if (session != null) { session.close(); } JdbcUtils.close(conn); } time = System.currentTimeMillis() - start; System.out.println("" + (time)); } }
From source file:com.glaf.core.test.MyBatisSessionFactoryTest.java
License:Apache License
public static void main(String[] args) { long start = System.currentTimeMillis(); SqlSessionFactory sqlSessionFactory = MyBatisSessionFactory.getSessionFactory(); SqlSession session = null; Connection conn = null;/*from w w w .j a v a 2s.com*/ try { conn = DBConnectionFactory.getConnection(); session = sqlSessionFactory.openSession(conn); SystemParam m = session.selectOne("getSystemParamById", "sys_table"); System.out.println(m.toJsonObject().toJSONString()); } catch (Exception ex) { ex.printStackTrace(); } finally { if (session != null) { session.close(); } JdbcUtils.close(conn); } long time = System.currentTimeMillis() - start; System.out.println("" + (time)); for (int i = 0; i < 20; i++) { start = System.currentTimeMillis(); session = null; conn = null; try { conn = DBConnectionFactory.getConnection("yz"); session = sqlSessionFactory.openSession(conn); SystemParam m = session.selectOne("getSystemParamById", "sys_table"); System.out.println(m.toJsonObject().toJSONString()); } catch (Exception ex) { ex.printStackTrace(); } finally { if (session != null) { session.close(); } JdbcUtils.close(conn); } time = System.currentTimeMillis() - start; System.out.println("" + (time)); } }
From source file:com.glaf.core.util.JdbcUtils.java
License:Apache License
public static void close(SqlSession session) { try {/*from ww w . j a v a 2 s . c om*/ if (session != null) { session.close(); } } catch (Exception ex) { } }
From source file:com.google.enterprise.connector.db.DBClient.java
License:Apache License
/** * @param skipRows number of rows to skip in the database. * @param maxRows max number of rows to return. * @return rows - subset of the result of executing the SQL query. E.g., * result table with columns id and lastName and two rows will be * returned as/*ww w . j ava 2 s . com*/ * * <pre> * [{id=1, lastName=last_01}, {id=2, lastName=last_02}] * </pre> * @throws DBException */ public List<Map<String, Object>> executePartialQuery(int skipRows, int maxRows) throws SnapshotRepositoryRuntimeException { // TODO(meghna): Think about a better way to scroll through the result set. List<Map<String, Object>> rows; LOG.info("Executing partial query with skipRows = " + skipRows + " and " + "maxRows = " + maxRows); SqlSession session = getSqlSession(); try { rows = session.selectList("IbatisDBClient.getAll", null, new RowBounds(skipRows, maxRows)); LOG.info( "Sucessfully executed partial query with skipRows = " + skipRows + " and maxRows = " + maxRows); } catch (RuntimeException e) { checkDBConnection(session, e); rows = new ArrayList<Map<String, Object>>(); } finally { session.close(); } LOG.info("Number of rows returned " + rows.size()); return rows; }
From source file:com.google.enterprise.connector.db.DBClient.java
License:Apache License
/** * Executes the partial parameterized query for given keyValue and * returns the list of records having their key value greater than keyValue * parameter.//from w w w . jav a2 s . co m * * @param keyValue * @return list of documents */ public List<Map<String, Object>> executeParameterizePartialQuery(Integer keyValue) throws SnapshotRepositoryRuntimeException { List<Map<String, Object>> rows; int skipRows = 0; int maxRows = dbContext.getNumberOfRows(); // Create a hashmap as to provide input parameters minvalue and maxvalue to // the query. Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("value", keyValue); LOG.info("Executing partial parametrized query with keyValue = " + keyValue); SqlSession session = getSqlSession(); try { rows = session.selectList("IbatisDBClient.getAll", paramMap, new RowBounds(skipRows, maxRows)); LOG.info("Sucessfully executed partial parametrized query with keyValue = " + keyValue); } catch (RuntimeException e) { checkDBConnection(session, e); rows = new ArrayList<Map<String, Object>>(); } finally { session.close(); } LOG.info("Number of rows returned " + rows.size()); return rows; }
From source file:com.google.enterprise.connector.db.DBClient.java
License:Apache License
/** * Returns information derived from the DatabaseMetaData. * * @param metaDataHandler a Function that takes a DatabaseMetaData as input * and returns a value/* w w w . j a va 2 s. c om*/ * @return the value returned by the metaDataHandler Function, or null if * there was an error */ public <T> T getDatabaseMetaData(SqlFunction<DatabaseMetaData, T> metaDataHandler) { try { SqlSession session = sqlSessionFactory.openSession(); try { Connection conn = session.getConnection(); try { DatabaseMetaData meta = conn.getMetaData(); if (meta != null) { return metaDataHandler.apply(meta); } } finally { conn.close(); } } finally { session.close(); } } catch (SQLException e) { LOG.warning("Caught SQLException while fetching database details: " + e); } catch (Exception e) { LOG.warning("Caught Exception while fetching database details: " + e); } return null; }
From source file:com.google.enterprise.connector.db.DBClient.java
License:Apache License
/** * Executes the AuthZ query for given user-name and list of * documents and returns the list of authorized documents. * * @param userName user-name/* w ww.ja v a 2s . c o m*/ * @param docIds List of documents to be authorized * @return list of authorized documents */ @SuppressWarnings("unchecked") public List<String> executeAuthZQuery(String userName, String docIds) { List<String> authorizedDocs = new ArrayList<String>(); // Create a hashmap as to provide input parameters userName and list of // documents to AuthZ query. Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("username", userName); paramMap.put("docIds", docIds); // Execute the AuthZ query. SqlSession session = getSqlSession(); try { authorizedDocs = session.selectList("IbatisDBClient.getAuthorizedDocs", paramMap); } catch (Exception e) { LOG.log(Level.WARNING, "Could not execute AuthZ query on the database.", e); } finally { session.close(); } return authorizedDocs; }
From source file:com.google.enterprise.connector.db.DBClient.java
License:Apache License
/** * Executes the Collation SQL query, to determine the sort order of the two * string values.// w w w.j av a 2 s.com * * @param source the source String * @param target the target String * @return an integer less than, equal to, or greater than zero depending * on whether the source string is less than, equal to, or greater than the * target string. */ public int executeCollationQuery(String source, String target) { // Determine which query to use based on DatabaseType or custom query. String collationQueryId = "IbatisDBClient.compareStrings"; if (!hasCustomCollationQuery) { if (databaseType == DatabaseType.ORACLE) { collationQueryId += "_" + databaseType.toString(); } } // Create a hashmap to provide input parameters to the query. Map<String, Object> paramMap = ImmutableMap.<String, Object>of("source", source, "target", target); // Execute the Collation query. SqlSession session = getSqlSession(); List<String> result; try { result = session.selectList(collationQueryId, paramMap); } catch (Exception e) { LOG.log(Level.WARNING, "Could not execute SQL Collation query.", e); // Fall back to local Java Collation. return Collator.getInstance().compare(source, target); } finally { session.close(); } // If the query returns two rows, the lesser value will be the first one. if (result.size() == 2) { return source.equals(result.get(0)) ? -1 : 1; } else { // If the query returns fewer than two rows, the strings were considered // equivalent; either through the UNION or the WHERE clause of the query. return 0; } }