List of usage examples for org.springframework.jdbc.core ConnectionCallback ConnectionCallback
ConnectionCallback
From source file:org.springframework.boot.actuate.autoconfigure.DataSourceMetricsAutoConfigurationTests.java
@Test public void multipleDataSources() { load(MultipleDataSourcesConfig.class); PublicMetrics bean = this.context.getBean(PublicMetrics.class); Collection<Metric<?>> metrics = bean.metrics(); assertMetrics(metrics, "datasource.tomcat.active", "datasource.tomcat.usage", "datasource.commonsDbcp.active", "datasource.commonsDbcp.usage"); // Hikari won't work unless a first connection has been retrieved JdbcTemplate jdbcTemplate = new JdbcTemplate(this.context.getBean("hikariDS", DataSource.class)); jdbcTemplate.execute(new ConnectionCallback<Void>() { @Override// w w w .j ava2 s . c om public Void doInConnection(Connection connection) throws SQLException, DataAccessException { return null; } }); Collection<Metric<?>> anotherMetrics = bean.metrics(); assertMetrics(anotherMetrics, "datasource.tomcat.active", "datasource.tomcat.usage", "datasource.hikariDS.active", "datasource.hikariDS.usage", "datasource.commonsDbcp.active", "datasource.commonsDbcp.usage"); }
From source file:org.springframework.boot.actuate.autoconfigure.PublicMetricsAutoConfigurationTests.java
@Test public void multipleDataSources() { load(MultipleDataSourcesConfig.class); PublicMetrics bean = this.context.getBean(DataSourcePublicMetrics.class); Collection<Metric<?>> metrics = bean.metrics(); assertMetrics(metrics, "datasource.tomcat.active", "datasource.tomcat.usage", "datasource.commonsDbcp.active", "datasource.commonsDbcp.usage"); // Hikari won't work unless a first connection has been retrieved JdbcTemplate jdbcTemplate = new JdbcTemplate(this.context.getBean("hikariDS", DataSource.class)); jdbcTemplate.execute(new ConnectionCallback<Void>() { @Override//from w w w. ja va 2s. co m public Void doInConnection(Connection connection) throws SQLException, DataAccessException { return null; } }); Collection<Metric<?>> anotherMetrics = bean.metrics(); assertMetrics(anotherMetrics, "datasource.tomcat.active", "datasource.tomcat.usage", "datasource.hikariDS.active", "datasource.hikariDS.usage", "datasource.commonsDbcp.active", "datasource.commonsDbcp.usage"); }
From source file:org.springframework.jdbc.core.JdbcTemplateTests.java
public void testConnectionCallback() throws Exception { replay();//w ww . jav a 2s . c o m JdbcTemplate template = new JdbcTemplate(mockDataSource); template.setNativeJdbcExtractor(new PlainNativeJdbcExtractor()); Object result = template.execute(new ConnectionCallback() { public Object doInConnection(Connection con) { assertSame(mockConnection, con); return "test"; } }); assertEquals("test", result); }
From source file:org.springframework.jdbc.core.JdbcTemplateTests.java
public void testConnectionCallbackWithStatementSettings() throws Exception { MockControl ctrlStatement = MockControl.createControl(PreparedStatement.class); PreparedStatement mockStatement = (PreparedStatement) ctrlStatement.getMock(); mockConnection.prepareStatement("some SQL"); ctrlConnection.setReturnValue(mockStatement, 1); mockStatement.setFetchSize(10);/* w ww . j a v a 2 s . c o m*/ ctrlStatement.setVoidCallable(1); mockStatement.setMaxRows(20); ctrlStatement.setVoidCallable(1); mockStatement.close(); ctrlStatement.setVoidCallable(1); replay(); JdbcTemplate template = new JdbcTemplate(mockDataSource); Object result = template.execute(new ConnectionCallback() { public Object doInConnection(Connection con) throws SQLException { PreparedStatement ps = con.prepareStatement("some SQL"); ps.close(); assertSame(mockConnection, new PlainNativeJdbcExtractor().getNativeConnection(con)); return "test"; } }); assertEquals("test", result); }
From source file:ru.org.linux.topic.TagDao.java
public void updateCounters(final List<String> oldTags, final List<String> newTags) { jdbcTemplate.execute(new ConnectionCallback<String>() { @Override/* ww w . j a v a 2s.c om*/ public String doInConnection(Connection con) throws SQLException, DataAccessException { PreparedStatement stInc = con .prepareStatement("UPDATE tags_values SET counter=counter+1 WHERE id=?"); PreparedStatement stDec = con .prepareStatement("UPDATE tags_values SET counter=counter-1 WHERE id=?"); for (String tag : newTags) { if (!oldTags.contains(tag)) { int id = getOrCreateTag(con, tag); stInc.setInt(1, id); stInc.executeUpdate(); } } for (String tag : oldTags) { if (!newTags.contains(tag)) { int id = getOrCreateTag(con, tag); stDec.setInt(1, id); stDec.executeUpdate(); } } return null; } }); }
From source file:ru.org.linux.topic.TagDao.java
public boolean updateTags(final int msgid, final List<String> tagList) { final List<String> oldTags = getMessageTags(msgid); return jdbcTemplate.execute(new ConnectionCallback<Boolean>() { @Override//from ww w .j a va2 s . c om public Boolean doInConnection(Connection con) throws SQLException, DataAccessException { PreparedStatement insertStatement = con.prepareStatement("INSERT INTO tags VALUES(?,?)"); PreparedStatement deleteStatement = con .prepareStatement("DELETE FROM tags WHERE msgid=? and tagid=?"); insertStatement.setInt(1, msgid); deleteStatement.setInt(1, msgid); boolean modified = false; for (String tag : tagList) { if (!oldTags.contains(tag)) { int id = getOrCreateTag(con, tag); insertStatement.setInt(2, id); insertStatement.executeUpdate(); modified = true; } } for (String tag : oldTags) { if (!tagList.contains(tag)) { int id = getOrCreateTag(con, tag); deleteStatement.setInt(2, id); deleteStatement.executeUpdate(); modified = true; } } insertStatement.close(); deleteStatement.close(); return modified; } }); }