@Override public void migrate(Connection connection) throws Exception { if ("sqlserver".equals(type) || "hsqldb".equals(type)) { //we don't have this problem with sqlserver or in memory DB logger.info("Skipping 4.0.4 migration for " + type + ", not affected by 3.9.9 back ports."); return; } ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); SingleConnectionDataSource dataSource = new SingleConnectionDataSource(connection, true); JdbcTemplate template = new JdbcTemplate(dataSource); boolean run = false; for (Map.Entry<String, String> script : getScripts()) { int count = template.queryForObject(checkExistsSql, Integer.class, script.getKey()); if (count == 0) { String path = "org/cloudfoundry/identity/uaa/db/" + type + "/" + script.getValue(); logger.info(String.format("[4.0.4] Adding script for version %s with path %s", script.getKey(), path)); populator.addScript(new ClassPathResource(path)); run = true; } } if (run) { logger.info("Running missing migrations."); populator.setContinueOnError(false); populator.setIgnoreFailedDrops(true); populator.populate(connection); logger.info("Completed missing migrations."); } else { logger.info("Skipping 4.0.4 migrations, no migrations missing."); } }
public void createDatabase() throws Exception { // Use a different connection url so the tests can run in parallel String connectionUrl; String sqlClassPathResource; if (!withAclClassIdType) { connectionUrl = "jdbc:hsqldb:mem:lookupstrategytest"; sqlClassPathResource = ACL_SCHEMA_SQL_FILE; } else { connectionUrl = "jdbc:hsqldb:mem:lookupstrategytestWithAclClassIdType"; sqlClassPathResource = ACL_SCHEMA_SQL_FILE_WITH_ACL_CLASS_ID; } dataSource = new SingleConnectionDataSource(connectionUrl, "sa", "", true); dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); jdbcTemplate = new JdbcTemplate(dataSource); Resource resource = new ClassPathResource(sqlClassPathResource); String sql = new String(FileCopyUtils.copyToByteArray(resource.getInputStream())); jdbcTemplate.execute(sql); }
@Test public void testLeaveConnectionOpenOnRequest() throws Exception { String sql = "SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3"; given(this.resultSet.next()).willReturn(false); given(this.connection.isClosed()).willReturn(false); given(this.connection.createStatement()).willReturn(this.preparedStatement); // if close is called entire test will fail willThrow(new RuntimeException()).given(this.connection).close(); SingleConnectionDataSource scf = new SingleConnectionDataSource(this.dataSource.getConnection(), false); this.template = new JdbcTemplate(scf, false); RowCountCallbackHandler rcch = new RowCountCallbackHandler(); this.template.query(sql, rcch); verify(this.resultSet).close(); verify(this.preparedStatement).close(); }
@Before public void setUp() throws SQLException { given(connection.createStatement()).willReturn(statement); given(connection.prepareStatement(anyString())).willReturn(preparedStatement); given(statement.executeQuery(anyString())).willReturn(resultSet); given(preparedStatement.executeQuery()).willReturn(resultSet); given(resultSet.next()).willReturn(true, true, false); given(resultSet.getString(1)).willReturn("tb1", "tb2"); given(resultSet.getInt(2)).willReturn(1, 2); template.setDataSource(new SingleConnectionDataSource(connection, false)); template.setExceptionTranslator(new SQLStateSQLExceptionTranslator()); template.afterPropertiesSet(); }
@SuppressWarnings("unchecked") public Mock(MockType type) throws Exception { connection = mock(Connection.class); statement = mock(Statement.class); resultSet = mock(ResultSet.class); resultSetMetaData = mock(ResultSetMetaData.class); given(connection.createStatement()).willReturn(statement); given(statement.executeQuery(anyString())).willReturn(resultSet); given(resultSet.getMetaData()).willReturn(resultSetMetaData); given(resultSet.next()).willReturn(true, false); given(resultSet.getString(1)).willReturn("Bubba"); given(resultSet.getLong(2)).willReturn(22L); given(resultSet.getTimestamp(3)).willReturn(new Timestamp(1221222L)); given(resultSet.getObject(anyInt(), any(Class.class))).willThrow(new SQLFeatureNotSupportedException()); given(resultSet.getDate(3)).willReturn(new java.sql.Date(1221222L)); given(resultSet.getBigDecimal(4)).willReturn(new BigDecimal("1234.56")); given(resultSet.wasNull()).willReturn(type == MockType.TWO); given(resultSetMetaData.getColumnCount()).willReturn(4); given(resultSetMetaData.getColumnLabel(1)).willReturn( type == MockType.THREE ? "Last Name" : "name"); given(resultSetMetaData.getColumnLabel(2)).willReturn("age"); given(resultSetMetaData.getColumnLabel(3)).willReturn("birth_date"); given(resultSetMetaData.getColumnLabel(4)).willReturn("balance"); jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setDataSource(new SingleConnectionDataSource(connection, false)); jdbcTemplate.setExceptionTranslator(new SQLStateSQLExceptionTranslator()); jdbcTemplate.afterPropertiesSet(); }
SingleConnectionDataSource dataSource = new SingleConnectionDataSource(); .... // The rest stays as is
// ... this code may be invoked in multiple threads simultaneously ... try(Connection conn = dao.getDataSource().getConnection()) { JdbcTemplate db = new JdbcTemplate(new SingleConnectionDataSource(conn, true)); // ... database-intensive code goes here ... // ... this code also is safe to run simultaneously in multiple threads ... // ... provided you are not creating new threads inside here }
HeartBeatDBSource() { try { connectionTimeoutExecutor = Executors.newFixedThreadPool(3); conn = dbf.getExtraDataSource().getConnection(); conn.setNetworkTimeout(connectionTimeoutExecutor, (int) TimeUnit.SECONDS.toMillis(PortalGlobalProperty.HEART_BEAT_QUERY_TIMEOUT)); source = new SingleConnectionDataSource(conn, true); jdbc = new JdbcTemplate(source); } catch (SQLException e) { throw new CloudRuntimeException(e); } }
public Dataset buildDataset(Map<String,Object> parameterMap,Connection conn){ String sqlForUse=sql; Context context=new Context(null,parameterMap); if(sqlExpression!=null){ sqlForUse=executeSqlExpr(sqlExpression, context); }else{ Pattern pattern=Pattern.compile("\\$\\{.*?\\}"); Matcher matcher=pattern.matcher(sqlForUse); while(matcher.find()){ String substr=matcher.group(); String sqlExpr=substr.substring(2,substr.length()-1); Expression expr=ExpressionUtils.parseExpression(sqlExpr); String result=executeSqlExpr(expr, context); sqlForUse=sqlForUse.replace(substr, result); } } Utils.logToConsole("RUNTIME SQL:"+sqlForUse); Map<String, Object> pmap = buildParameters(parameterMap); if(ProcedureUtils.isProcedure(sqlForUse)){ List<Map<String,Object>> result = ProcedureUtils.procedureQuery(sqlForUse,pmap,conn); return new Dataset(name,result); } SingleConnectionDataSource datasource=new SingleConnectionDataSource(conn,false); NamedParameterJdbcTemplate jdbcTemplate=new NamedParameterJdbcTemplate(datasource); List<Map<String,Object>> list= jdbcTemplate.queryForList(sqlForUse, pmap); return new Dataset(name,list); }
protected JdbcTemplate getJdbcTemplate(Connection connection) { DataSource dataSource = new SingleConnectionDataSource(connection, true); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); return jdbcTemplate; }
private JdbcTemplate getJdbcTemplate(Connection connection) { DataSource dataSource = new SingleConnectionDataSource(connection, true); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); return jdbcTemplate; }
protected JdbcTemplate getJdbcTemplate(Connection connection) { DataSource dataSource = new SingleConnectionDataSource(connection, true); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); return jdbcTemplate; }
protected JdbcTemplate createJdbcTemplate(Connection conn) { try { return new JdbcTemplate(new SingleConnectionDataSource(conn, true)); } catch (Exception e) { throw e instanceof IdException ? (IdException) e : new IdException(e); } }
public static DataSource register(DataSource dataSource) { Connection conn; try { conn = dataSource.getConnection(); } catch (SQLException e) { throw new RuntimeException(e.getMessage(), e); } list.add(conn); return new SingleConnectionDataSource(conn, false); }
public void execute(Connection connection) { try { springJdbcMigration.migrate(new org.springframework.jdbc.core.JdbcTemplate( new SingleConnectionDataSource(connection, true))); } catch (Exception e) { throw new FlywayException("Migration failed !", e); } } }
@Override public void afterPropertiesSet() throws Exception { Assert.notNull(hiveDataSource, "HiveDataSource must be set"); Connection con = DataSourceUtils.getConnection(hiveDataSource); factoryDataSource = new SingleConnectionDataSource(con, true); }
public static SingleConnectionDataSource getDataSourceFrom(String databaseFile) { SingleConnectionDataSource dataSource = new SingleConnectionDataSource(); dataSource.setDriverClassName("org.sqlite.JDBC"); dataSource.setUrl("jdbc:sqlite:" + databaseFile); return dataSource; }
@Bean public DataSource jdbcDataSource() { SingleConnectionDataSource ds = new SingleConnectionDataSource(); ds.setDriverClassName("org.sqlite.JDBC"); ds.setUrl("jdbc:sqlite:stats.db"); return ds; }
@Override protected JdbcTemplate createJdbcTemplate() { SingleConnectionDataSource ds = new SingleConnectionDataSource(); ds.setUrl(JDBC_URL_BASE + getDbFile()); ds.setSuppressClose(true); return new JdbcTemplate(ds); }
SingleConnectionDataSource ds = new SingleConnectionDataSource(); ds.setDriverClassName("org.hsqldb.jdbcDriver"); ds.setUrl("jdbc:hsqldb:data/tutorial"); ds.setUsername("sa"); ds.setPassword(""); JdbcTemplate jt = new JdbcTemplate(ds); jt.execute("insert into employee (id, name) values (?, ?)", 1, "Tom");