/** * <p>Start new transaction.</p> * @throws Exception - an exception **/ @Override public final void beginTransaction() throws Exception { getOrEstablishConnection(); }
/** * <p>Get SrvDatabase in lazy mode.</p> * @return SrvDatabase - SrvDatabase * @throws Exception - an exception */ @Override public final synchronized SrvDatabase lazyGetSrvDatabase() throws Exception { if (this.srvDatabase == null) { this.srvDatabase = new SrvDatabase(); this.srvDatabase.setLogger(lazyGetLogger()); this.srvDatabase.setHlpInsertUpdate(lazyGetHlpInsertUpdate()); this.srvDatabase.setDataSource(lazyGetDataSource()); this.srvDatabase.setSrvRecordRetriever(lazyGetSrvRecordRetriever()); lazyGetLogger().info(AFactoryAppBeans.class, "SrvDatabase has been created."); } return this.srvDatabase; }
/** * <p>Rollback transaction to savepoint.</p> * @param pSavepointName savepoint name * @throws Exception - an exception **/ @Override public final void rollBackTransaction(final String pSavepointName) throws Exception { getLogger().debug(SrvDatabase.class, "try to rollback to savepoint: " + pSavepointName); this.threadConnection.get(). rollback(savepointsMap.get(pSavepointName)); savepointsMap.remove(pSavepointName); }
/** * <p>Execute any SQL query that returns no data. * E.g. PRAGMA, etc.</p> * @param pQuery query * @throws Exception - an exception **/ @Override public final void executeQuery(final String pQuery) throws Exception { Statement stmt = null; try { getLogger().debug(SrvDatabase.class, "Thread ID=" + Thread.currentThread() .getId() + ", try to execute query: " + pQuery); stmt = getOrEstablishConnection().createStatement(); stmt.executeUpdate(pQuery); } catch (SQLException sqle) { String msg = sqle.getMessage() + ", RDBMS error code " + sqle.getErrorCode() + ", query:\n" + pQuery; throw new ExceptionWithCode(ASrvDatabase.SQL_EXEC_ERROR, msg); } finally { if (stmt != null) { stmt.close(); } } }
/** * <p>Execute SQL INSERT that returns affected rows. * It is to adapt Android insert/update/delete interface. * </p> * @param pTable table name * @param pColumnsVals type-safe map column name - column value * @return row count affected * @throws Exception - an exception **/ @Override public final long executeInsert(final String pTable, final ColumnsValues pColumnsVals) throws Exception { Statement stmt = null; String query = getHlpInsertUpdate().evalSqlInsert(pTable, pColumnsVals); try { getLogger().debug(SrvDatabase.class, "Thread ID=" + Thread.currentThread() .getId() + ", try to execute insert: " + query); stmt = getOrEstablishConnection().createStatement(); return stmt.executeUpdate(query); } catch (SQLException sqle) { String msg = sqle.getMessage() + ", RDBMS error code " + sqle.getErrorCode() + ", query:\n" + query; throw new ExceptionWithCode(ASrvDatabase.SQL_EXEC_ERROR, msg); } finally { if (stmt != null) { stmt.close(); } } }
/** * <p>Get SrvRecordRetriever in lazy mode.</p> * @return SrvRecordRetriever - SrvRecordRetriever * @throws Exception - an exception */ @Override public final synchronized SrvRecordRetriever lazyGetSrvRecordRetriever() throws Exception { if (this.srvRecordRetriever == null) { this.srvRecordRetriever = new SrvRecordRetriever(); lazyGetLogger().info(AFactoryAppBeans.class, "SrvRecordRetriever has been created."); } return this.srvRecordRetriever; }
/** * <p>Get SrvDatabase in lazy mode.</p> * @return SrvDatabase - SrvDatabase * @throws Exception - an exception */ @Override public final synchronized SrvDatabase lazyGetSrvDatabase() throws Exception { String beanName = getSrvDatabaseName(); SrvDatabase srvDatabase = (SrvDatabase) getBeansMap().get(beanName); if (srvDatabase == null) { srvDatabase = new SrvDatabase(); srvDatabase.setLogger(lazyGetLogger()); srvDatabase.setHlpInsertUpdate(lazyGetHlpInsertUpdate()); srvDatabase.setDataSource(lazyGetDataSource()); getBeansMap().put(beanName, srvDatabase); lazyGetLogger().info(null, AFactoryAppBeansJdbc.class, beanName + " has been created."); } return srvDatabase; } }
getLogger().debug(SrvDatabase.class, "Thread ID=" + Thread.currentThread() .getId() + ", try to execute delete: " + query); stmt = getOrEstablishConnection().createStatement(); return stmt.executeUpdate(query); } catch (SQLException sqle) {
/** * <p>Start new transaction.</p> * @throws Exception - an exception **/ @Override public final void beginTransaction() throws Exception { getOrEstablishConnection(); }
/** * <p>Rollback transaction to savepoint.</p> * @param pSavepointName savepoint name * @throws Exception - an exception **/ @Override public final void rollBackTransaction(final String pSavepointName) throws Exception { if (getLogger().getIsShowDebugMessagesFor(getClass())) { getLogger().debug(null, SrvDatabase.class, "try to rollback to savepoint: " + pSavepointName); } this.threadConnection.get(). rollback(savepointsMap.get(pSavepointName)); savepointsMap.remove(pSavepointName); }
/** * <p>Retrieve records from DB.</p> * @param pSelect query SELECT * @return IRecordSet record set * @throws Exception - an exception **/ @Override public final IRecordSet<ResultSet> retrieveRecords(final String pSelect) throws Exception { try { getLogger().debug(SrvDatabase.class, "Thread ID=" + Thread.currentThread() .getId() + ", try to retrieve records: " + pSelect); final Statement stmt = getOrEstablishConnection().createStatement(); final ResultSet rs = stmt.executeQuery(pSelect); return new RecordSetJdbc(rs, stmt); } catch (SQLException sqle) { String msg = sqle.getMessage() + ", RDBMS error code " + sqle.getErrorCode() + ", query:\n" + pSelect; throw new ExceptionWithCode(ASrvDatabase.SQL_EXEC_ERROR, msg); } }
/** * <p>Get if an transaction is started.</p> * @return if autocommit * @throws Exception - an exception **/ @Override public final boolean getIsAutocommit() throws Exception { return getOrEstablishConnection().getAutoCommit(); }
/** * <p>Execute any SQL query that returns no data. * E.g. PRAGMA, etc.</p> * @param pQuery query * @throws Exception - an exception **/ @Override public final void executeQuery(final String pQuery) throws Exception { Statement stmt = null; try { if (getLogger().getIsShowDebugMessagesFor(getClass()) && getLogger().getDetailLevel() > 30000) { getLogger().debug(null, SrvDatabase.class, "Thread ID=" + Thread.currentThread().getId() + ", try to execute query: " + pQuery); } stmt = getOrEstablishConnection().createStatement(); stmt.executeUpdate(pQuery); } catch (SQLException sqle) { String msg = sqle.getMessage() + ", RDBMS error code " + sqle.getErrorCode() + ", query:\n" + pQuery; throw new ExceptionWithCode(ASrvDatabase.SQL_EXEC_ERROR, msg); } finally { if (stmt != null) { stmt.close(); } } }
/** * <p>Set RDBMS autocommit mode.</p> * @param pIsAutocommit if autocommit * @throws Exception - an exception **/ @Override public final void setIsAutocommit( final boolean pIsAutocommit) throws Exception { getOrEstablishConnection().setAutoCommit(pIsAutocommit); }
/** * <p>Get if an transaction is started.</p> * @param pIsAutocommit is autocommit * @throws Exception - an exception **/ @Override public final boolean getIsAutocommit() throws Exception { return getOrEstablishConnection().getAutoCommit(); }
/** * <p>Set RDBMS autocommit mode.</p> * @param pIsAutocommit if autocommit * @throws Exception - an exception **/ @Override public final void setIsAutocommit( final boolean pIsAutocommit) throws Exception { getOrEstablishConnection().setAutoCommit(pIsAutocommit); }
/** * <p>Get transaction isolation level. * You must {@link #setTransactionIsolation(int) setTransactionIsolation} * before invoke it cause a RDBMS may has any level by default.</p> * @return pLevel according ASrvDatabase * @throws Exception - an exception **/ @Override public final int getTransactionIsolation() throws Exception { int level = getOrEstablishConnection().getTransactionIsolation(); if (level == Connection.TRANSACTION_READ_UNCOMMITTED) { return ASrvDatabase.TRANSACTION_READ_UNCOMMITTED; } else if (level == Connection.TRANSACTION_READ_COMMITTED) { return ASrvDatabase.TRANSACTION_READ_COMMITTED; } else if (level == Connection.TRANSACTION_REPEATABLE_READ) { return ASrvDatabase.TRANSACTION_REPEATABLE_READ; } else if (level == Connection.TRANSACTION_SERIALIZABLE) { return ASrvDatabase.TRANSACTION_SERIALIZABLE; } else if (level == Connection.TRANSACTION_NONE) { return ASrvDatabase.TRANSACTION_NONE; } throw new ExceptionWithCode(ASrvDatabase.BAD_PARAMS, "Unknown JDBC isolation level!"); }
/** * <p>Get transaction isolation level. * You must {@link #setTransactionIsolation(int) setTransactionIsolation} * before invoke it cause a RDBMS may has any level by default.</p> * @return pLevel according ASrvDatabase * @throws Exception - an exception **/ @Override public final int getTransactionIsolation() throws Exception { int level = getOrEstablishConnection().getTransactionIsolation(); if (level == Connection.TRANSACTION_READ_UNCOMMITTED) { return ASrvDatabase.TRANSACTION_READ_UNCOMMITTED; } else if (level == Connection.TRANSACTION_READ_COMMITTED) { return ASrvDatabase.TRANSACTION_READ_COMMITTED; } else if (level == Connection.TRANSACTION_REPEATABLE_READ) { return ASrvDatabase.TRANSACTION_REPEATABLE_READ; } else if (level == Connection.TRANSACTION_SERIALIZABLE) { return ASrvDatabase.TRANSACTION_SERIALIZABLE; } else if (level == Connection.TRANSACTION_NONE) { return ASrvDatabase.TRANSACTION_NONE; } throw new ExceptionWithCode(ASrvDatabase.BAD_PARAMS, "Unknown JDBC isolation level!"); }
/** * <p>Set transaction isolation level.</p> * @param pLevel according ASrvDatabase * @throws Exception - an exception **/ @Override public final void setTransactionIsolation( final int pLevel) throws Exception { getOrEstablishConnection(); if (pLevel == ASrvDatabase.TRANSACTION_READ_UNCOMMITTED) { this.threadConnection.get() .setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); } else if (pLevel == ASrvDatabase.TRANSACTION_READ_COMMITTED) { this.threadConnection.get() .setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); } else if (pLevel == ASrvDatabase.TRANSACTION_REPEATABLE_READ) { this.threadConnection.get() .setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); } else if (pLevel == ASrvDatabase.TRANSACTION_SERIALIZABLE) { this.threadConnection.get() .setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); } else if (pLevel == ASrvDatabase.TRANSACTION_NONE) { this.threadConnection.get() .setTransactionIsolation(Connection.TRANSACTION_NONE); } else { throw new ExceptionWithCode(ASrvDatabase.BAD_PARAMS, "Unknown isolation level parameter!"); } }
/** * <p>Set transaction isolation level.</p> * @param pLevel according ASrvDatabase * @throws Exception - an exception **/ @Override public final void setTransactionIsolation( final int pLevel) throws Exception { getOrEstablishConnection(); if (pLevel == ASrvDatabase.TRANSACTION_READ_UNCOMMITTED) { this.threadConnection.get() .setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); } else if (pLevel == ASrvDatabase.TRANSACTION_READ_COMMITTED) { this.threadConnection.get() .setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); } else if (pLevel == ASrvDatabase.TRANSACTION_REPEATABLE_READ) { this.threadConnection.get() .setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); } else if (pLevel == ASrvDatabase.TRANSACTION_SERIALIZABLE) { this.threadConnection.get() .setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); } else if (pLevel == ASrvDatabase.TRANSACTION_NONE) { this.threadConnection.get() .setTransactionIsolation(Connection.TRANSACTION_NONE); } else { throw new ExceptionWithCode(ASrvDatabase.BAD_PARAMS, "Unknown isolation level parameter!"); } }