/** * @return datasource wrapped in the database operator. */ public AzkabanDataSource getDataSource() { return (AzkabanDataSource) this.queryRunner.getDataSource(); } }
/** * Note: this queryRunner should include a concrete {@link AzkabanDataSource} inside. */ @Inject public DatabaseOperator(final QueryRunner queryRunner) { requireNonNull(queryRunner.getDataSource(), "data source must not be null."); this.queryRunner = queryRunner; }
@Override public void writeUndoLog(UndoLogDO undoLogDo) throws SQLException { Connection connection = queryRunner.getDataSource().getConnection(); writeUndoLogByGivenConnection(connection, undoLogDo); DbUtils.close(connection); }
/** * Provide a way to allow users define custom SQL operations without relying on fixed SQL * interface. The common use case is to group a sequence of SQL operations without commit every * time. * * @param operations A sequence of DB operations * @param <T> The type of object that the operations returns. Note that T could be null * @return T The object returned by the SQL statement, expected by the caller */ public <T> T transaction(final SQLTransaction<T> operations) throws SQLException { Connection conn = null; try { conn = this.queryRunner.getDataSource().getConnection(); conn.setAutoCommit(false); final DatabaseTransOperator transOperator = new DatabaseTransOperator(this.queryRunner, conn); final T res = operations.execute(transOperator); conn.commit(); return res; } catch (final SQLException ex) { // todo kunkun-tang: Retry logics should be implemented here. logger.error("transaction failed", ex); if (this.dbMetrics != null) { this.dbMetrics.markDBFailTransaction(); } throw ex; } finally { DbUtils.closeQuietly(conn); } }
rollbackInfo.setRollbackSqlList(undoLogDOList); txLogger.trace(groupId, unitId, "txc", "rollbackInfo sql " + rollbackInfo.toString()); connection = queryRunner.getDataSource().getConnection(); undoRollbackInfoSql(connection, rollbackInfo); } finally {
@Before public void setUp() throws Exception { this.queryRunner = mock(QueryRunner.class); this.conn = this.datasource.getConnection(); final DataSource mockDataSource = mock(this.datasource.getClass()); when(this.queryRunner.getDataSource()).thenReturn(mockDataSource); when(mockDataSource.getConnection()).thenReturn(this.conn); this.dbOperator = new DatabaseOperator(this.queryRunner); list.add(index_1); list.add(index_2); // valid query returns correct value when(this.queryRunner.query("select * from blah where ? = ?", this.handler, "id", 2)) .thenReturn(index_2); // If select an non-existing entry, handler returns 0. when(this.queryRunner.query("select * from blah where ? = ?", this.handler, "id", 3)) .thenReturn(0); //If typos, throw Exceptions. doThrow(SQLException.class).when(this.queryRunner) .query("sele * from blah where ? = ?", this.handler, "id", 2); doAnswer(invocation -> { index_1 = 26; return 1; }).when(this.queryRunner).update("update blah set ? = ?", "1", 26); }
public static DataSource getDataSource(Properties properties) { if (dataSource == null) { try { dataSource = BasicDataSourceFactory.createDataSource(properties); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } queryRunner = new QueryRunner(dataSource, false); try { dataSource.setValidationQuery("select 1"); // http://stackoverflow.com/questions/26404283/java-hibernate-with-sql-server-2012-not-working/27847317#27847317 connection = queryRunner.getDataSource().getConnection(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return dataSource; }
public static DataSource getDataSource(DatabaseDriver driver, String domain, String serverName, String defaultDatabaseName, String username, String password) { if (dataSource == null) { dataSource = new BasicDataSource(); dataSource.setDriverClassName(driver.getdriverClassName()); String driverurl = driver.getdriverUrl() + "://" + serverName + "/" + defaultDatabaseName; String NTLMStr = (StringUtils.isNotEmpty(domain)) ? ";integrated security=false;useNTLMv2=true;domain=" + domain : ";useKerberos=true;"; dataSource.setUrl(driverurl + NTLMStr); dataSource.setUsername(username); dataSource.setPassword(password); // set the auto commit for transaction dataSource.setMaxTotal(100); dataSource.setDefaultAutoCommit(true); } queryRunner = new QueryRunner(dataSource, false); try { dataSource.setValidationQuery("select 1"); // http://stackoverflow.com/questions/26404283/java-hibernate-with-sql-server-2012-not-working/27847317#27847317 connection = queryRunner.getDataSource().getConnection(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return dataSource; }