List of usage examples for java.sql Connection setAutoCommit
void setAutoCommit(boolean autoCommit) throws SQLException;
From source file:com.aurel.track.dbase.Migrate502To503.java
private static void addDuration(Integer durationField, String name, String fieldType, String label, String tooltip) {// ww w. j ava2s . c om TFieldBean durationFieldBean = FieldBL.loadByPrimaryKey(durationField); if (durationFieldBean == null) { LOGGER.info("Adding duration field " + durationField); Connection cono = null; try { cono = InitDatabase.getConnection(); Statement ostmt = cono.createStatement(); cono.setAutoCommit(false); ostmt.executeUpdate(addField(durationField, name, fieldType)); ostmt.executeUpdate(addFieldConfig(durationField, durationField, label, tooltip)); cono.commit(); cono.setAutoCommit(true); } catch (Exception e) { LOGGER.debug(ExceptionUtils.getStackTrace(e)); } finally { try { if (cono != null) { cono.close(); } } catch (Exception e) { LOGGER.debug(ExceptionUtils.getStackTrace(e)); } } } }
From source file:com.wavemaker.runtime.data.util.JDBCUtils.java
public static Object runSql(String sql[], String url, String username, String password, String driverClassName, Log logger, boolean isDDL) { Connection con = getConnection(url, username, password, driverClassName); try {//from ww w . j a va2 s. c o m try { con.setAutoCommit(true); } catch (SQLException ex) { throw new DataServiceRuntimeException(ex); } Statement s = con.createStatement(); try { try { for (String stmt : sql) { if (logger != null && logger.isInfoEnabled()) { logger.info("Running " + stmt); } s.execute(stmt); } if (!isDDL) { ResultSet r = s.getResultSet(); List<Object> rtn = new ArrayList<Object>(); while (r.next()) { // getting only the first col is pretty unuseful rtn.add(r.getObject(1)); } return rtn.toArray(new Object[rtn.size()]); } } catch (Exception ex) { if (logger != null && logger.isErrorEnabled()) { logger.error(ex.getMessage()); } throw ex; } } finally { try { s.close(); } catch (Exception ignore) { } } } catch (Exception ex) { if (ex instanceof RuntimeException) { throw (RuntimeException) ex; } else { throw new DataServiceRuntimeException(ex); } } finally { try { con.close(); } catch (Exception ignore) { } } return null; }
From source file:com.hangum.tadpole.engine.manager.TadpoleSQLTransactionManager.java
/** * java.sql.connection? ? ./*from w w w . java 2 s . co m*/ * * @param userId * @param userDB * * @return * @throws Exception */ public static Connection getInstance(final String userId, final UserDBDAO userDB) throws Exception { if (logger.isDebugEnabled()) logger.debug("[userId]" + userId + "[userDB]" + userDB.getUrl() + " / " + userDB.getUsers()); synchronized (dbManager) { final String searchKey = getKey(userId, userDB); TransactionDAO transactionDAO = dbManager.get(searchKey); if (transactionDAO == null) { try { DataSource ds = DBCPConnectionManager.getInstance().getDataSource(userId, userDB); transactionDAO = new TransactionDAO(); Connection conn = ds.getConnection(); conn.setAutoCommit(false); transactionDAO.setConn(conn); transactionDAO.setUserId(userId); transactionDAO.setUserDB(userDB); transactionDAO.setStartTransaction(new Date(System.currentTimeMillis())); transactionDAO.setKey(searchKey); dbManager.put(searchKey, transactionDAO); if (logger.isDebugEnabled()) logger.debug("\t New connection SQLMapSession."); } catch (Exception e) { logger.error("transaction connection", e); } } else { if (logger.isDebugEnabled()) { logger.debug("\t Already register SQLMapSession."); logger.debug("\t Is auto commit " + transactionDAO.getConn().getAutoCommit()); } } if (logger.isDebugEnabled()) logger.debug("[conn code]" + transactionDAO.toString()); return transactionDAO.getConn(); } }
From source file:com.aurel.track.dbase.Migrate411To412.java
public static void addTaskIsMilestone() { TFieldBean taskIsMilestoneFieldBean = FieldBL.loadByPrimaryKey(SystemFields.INTEGER_TASK_IS_MILESTONE); if (taskIsMilestoneFieldBean == null) { LOGGER.info("Adding task is milestone field"); Connection cono = null; try {// ww w .ja va 2 s . com cono = InitDatabase.getConnection(); Statement ostmt = cono.createStatement(); cono.setAutoCommit(false); ostmt.executeUpdate(addField(SystemFields.INTEGER_TASK_IS_MILESTONE, "TaskIsMilestone", "com.aurel.track.fieldType.types.system.check.SystemTaskIsMilestone")); ostmt.executeUpdate(addFieldConfig(SystemFields.INTEGER_TASK_IS_MILESTONE, SystemFields.INTEGER_TASK_IS_MILESTONE, "Task is milestone", "Is milestone")); cono.commit(); cono.setAutoCommit(true); } catch (Exception e) { LOGGER.debug(ExceptionUtils.getStackTrace(e)); } finally { try { if (cono != null) { cono.close(); } } catch (Exception e) { LOGGER.debug(ExceptionUtils.getStackTrace(e)); } } } }
From source file:com.aurel.track.dbase.Migrate411To412.java
/** * Add the document and document section * Use JDBC because negative objectIDs should be added *//* www .ja va2 s.com*/ public static void addNewItemTypes() { LOGGER.info("Add new item types"); List<String> itemTypeStms = new ArrayList<String>(); TListTypeBean docIssueTypeBean = IssueTypeBL.loadByPrimaryKey(-4); if (docIssueTypeBean == null) { LOGGER.info("Add 'Document' item type"); itemTypeStms.add(addItemtype(-4, "Document", 4, -4, "document.png", "2007")); } TListTypeBean docSectionIssueTypeBean = IssueTypeBL.loadByPrimaryKey(-5); if (docSectionIssueTypeBean == null) { LOGGER.info("Add 'Document section' item type"); itemTypeStms.add(addItemtype(-5, "Document section", 5, -5, "documentSection.png", "2008")); } Connection cono = null; try { cono = InitDatabase.getConnection(); Statement ostmt = cono.createStatement(); cono.setAutoCommit(false); for (String filterClobStmt : itemTypeStms) { ostmt.executeUpdate(filterClobStmt); } cono.commit(); cono.setAutoCommit(true); } catch (Exception e) { LOGGER.debug(ExceptionUtils.getStackTrace(e)); } finally { try { if (cono != null) { cono.close(); } } catch (Exception e) { LOGGER.info("Closing the connection failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); } } //children types List<TChildIssueTypeBean> childIssueTypeBeans = ChildIssueTypeAssignmentsBL .loadByChildAssignmentsByParent(-4); if (childIssueTypeBeans == null || childIssueTypeBeans.isEmpty()) { //document may have only document section children ChildIssueTypeAssignmentsBL.save(-4, -5); } TListTypeBean actionItemIssueTypeBean = IssueTypeBL.loadByPrimaryKey(5); TListTypeBean meetingIssueTypeBean = IssueTypeBL.loadByPrimaryKey(-3); if (actionItemIssueTypeBean != null && meetingIssueTypeBean != null) { //action item exists childIssueTypeBeans = ChildIssueTypeAssignmentsBL.loadByChildAssignmentsByParent(-3); if (childIssueTypeBeans == null || childIssueTypeBeans.isEmpty()) { //meeting may have only action item children ChildIssueTypeAssignmentsBL.save(-3, 5); } } }
From source file:com.hangum.tadpole.rdb.core.editors.main.utils.plan.CubridExecutePlanUtils.java
/** * cubrid execute plan/*from ww w . ja v a 2s .com*/ * * @param userDB * @param reqQuery * @return * @throws Exception */ public static String plan(UserDBDAO userDB, final RequestQuery reqQuery) throws Exception { String sql = reqQuery.getSql(); if (!sql.toLowerCase().startsWith("select")) { logger.error("[cubrid execute plan ]" + sql); throw new Exception("This statment not select. please check."); } Connection conn = null; ResultSet rs = null; PreparedStatement pstmt = null; try { conn = TadpoleSQLManager.getInstance(userDB).getDataSource().getConnection(); conn.setAutoCommit(false); // auto commit? false . sql = StringUtils.trim(sql).substring(6); if (logger.isDebugEnabled()) logger.debug("[qubrid modifying query]" + sql); sql = "select " + RECOMPILE + sql; pstmt = conn.prepareStatement(sql); if (reqQuery.getSqlStatementType() == SQL_STATEMENT_TYPE.PREPARED_STATEMENT) { final Object[] statementParameter = reqQuery.getStatementParameter(); for (int i = 1; i <= statementParameter.length; i++) { pstmt.setObject(i, statementParameter[i - 1]); } } ((CUBRIDStatement) pstmt).setQueryInfo(true); rs = pstmt.executeQuery(); String plan = ((CUBRIDStatement) pstmt).getQueryplan(); // ? . if (logger.isDebugEnabled()) logger.debug("cubrid plan text : " + plan); return plan; } finally { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } }
From source file:gridool.mapred.db.task.DBMapShuffleTaskBase.java
private static void configureConnection(final Connection conn) { try {//from w w w. j a va 2 s. c o m conn.setReadOnly(true); // should *not* call setReadOnly in a transaction (for MonetDB) conn.setAutoCommit(false); } catch (SQLException e) { LOG.warn("failed to configure a connection", e); } }
From source file:com.ibm.research.rdf.store.runtime.service.sql.UpdateHelper.java
private static String executeCall(Connection conn, String sql, int retPid, Object... params) { CallableStatement stmt = null; String ret = null;// ww w .java2 s . c om try { conn.setAutoCommit(false); } catch (SQLException ex) { log.error(ex); ex.printStackTrace(); System.out.println(ex.getLocalizedMessage()); return ret; } try { stmt = conn.prepareCall(sql); int i = 1; for (Object o : params) { stmt.setObject(i, o); i++; } stmt.registerOutParameter(retPid, Types.VARCHAR); stmt.execute(); ret = stmt.getString(retPid); conn.commit(); } catch (SQLException e) { // log.error(e); // e.printStackTrace(); // System.out.println(e.getLocalizedMessage()); ret = null; try { conn.rollback(); } catch (SQLException e1) { // TODO Auto-generated catch block log.error(e1); e1.printStackTrace(); System.out.println(e1.getLocalizedMessage()); ret = null; } } finally { closeSQLObjects(stmt, null); } try { conn.setAutoCommit(true); } catch (SQLException ex) { log.error(ex); ex.printStackTrace(); System.out.println(ex.getLocalizedMessage()); ret = null; } return ret; }
From source file:com.webbfontaine.valuewebb.gtns.TTGTNSSynchronizer.java
private static void updateTTFlag(TtGen ttGen) { Connection conn = null; PreparedStatement ps = null;/*from w ww.j a v a 2s. c o m*/ try { conn = Utils.getSQLConnection(); conn.setAutoCommit(false); ps = conn.prepareStatement(UPDATE_TT_SQL); ps.setLong(1, ttGen.getId()); ps.executeUpdate(); conn.commit(); } catch (SQLException e) { LOGGER.error("Error in updating GCNet Submission Flag for TT with id {0}, error {1}", ttGen.getId(), e); } finally { DBUtils.closeResource(ps); DBUtils.closeResource(conn); } }
From source file:com.clustercontrol.commons.util.JdbcBatchExecutor.java
/** * ?//from w w w . jav a 2 s .c om * * @param query * insert???update */ public static void execute(List<JdbcBatchQuery> queryList) { Connection conn = null; long start = HinemosTime.currentTimeMillis(); JpaTransactionManager tm = null; try { tm = new JpaTransactionManager(); conn = tm.getEntityManager().unwrap(java.sql.Connection.class); conn.setAutoCommit(false); for (JdbcBatchQuery query : queryList) try (PreparedStatement pstmt = conn.prepareStatement(query.getSql())) { query.addBatch(pstmt); pstmt.executeBatch(); } if (!tm.isNestedEm()) { conn.commit(); } } catch (Exception e) { log.warn(e); if (conn != null) { try { conn.rollback(); } catch (SQLException e1) { log.warn(e1); } } } finally { if (tm != null) { tm.close(); } } long time = HinemosTime.currentTimeMillis() - start; String className = ""; if (queryList.size() != 0) { className = queryList.get(0).getClass().getSimpleName(); } String message = String.format("Execute [%s] batch: %dms. size=%d", className, time, queryList.size()); if (time > 3000) { log.warn(message); } else if (time > 1000) { log.info(message); } else { log.debug(message); } }