- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
据我了解,在 Java 中使用 PreparedStatement 是我们可以多次使用它。但是我在使用 Spring JDBC 的 PreparedStatementCreator 时有些困惑。
例如考虑以下代码,
public class SpringTest {
JdbcTemplate jdbcTemplate;
PreparedStatementCreator preparedStatementCreator;
ResultSetExtractor<String> resultSetExtractor;
public SpringTest() throws SQLException {
jdbcTemplate = new JdbcTemplate(OracleUtil.getDataSource());
preparedStatementCreator = new PreparedStatementCreator() {
String query = "select NAME from TABLE1 where ID=?";
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
return connection.prepareStatement(query);
}
};
resultSetExtractor = new ResultSetExtractor<String>() {
public String extractData(ResultSet resultSet) throws SQLException,
DataAccessException {
if (resultSet.next()) {
return resultSet.getString(1);
}
return null;
}
};
}
public String getNameFromId(int id){
return jdbcTemplate.query(preparedStatementCreator, new Table1Setter(id), resultSetExtractor);
}
private static class Table1Setter implements PreparedStatementSetter{
private int id;
public Table1Setter(int id) {
this.id =id;
}
@Override
public void setValues(PreparedStatement preparedStatement) throws SQLException {
preparedStatement.setInt(1, id);
}
}
public static void main(String[] args) {
try {
SpringTest springTest = new SpringTest();
for(int i=0;i<10;i++){
System.out.println(springTest.getNameFromId(i));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
根据这段代码,当我调用 springTest.getNameFromId(int id) 方法时,它从给定的 id 返回名称,这里我使用 PreparedStatementCreator 创建 PreparedStatement 并使用 PreparedStatementSetter 设置输入参数,我从 ResultSetExtractor 得到结果。但是性能很慢。
在调试并查看 PreparedStatementCreator 和 JdbcTemplate 内部发生的情况后,我知道 PreparedStatementCreator 每次都会创建新的 PreparedStatement...!!!
每当我调用方法 jdbcTemplate.query(preparedStatementCreator,preparedStatementSetter,resultSetExtractor) 时,它都会创建新的 PreparedStatement,这会降低性能。
这是使用 PreparedStatementCreator 的正确方法吗?因为在这段代码中我无法重用 PreparedStatement。如果这是使用 PreparedStatementCreator 的正确方法,那么如何从 PreparedStatement 的可重用性中获益?
最佳答案
Prepared Statements 通常由底层连接池缓存,因此您不必担心每次都创建一个新的。
所以我认为你的实际用法是正确的。
JdbcTemplate 在执行后关闭语句,所以如果你真的想重用同一个准备好的语句,你可以代理语句并在语句创建器中拦截 close 方法
例如(未测试,仅作为示例):
public abstract class ReusablePreparedStatementCreator implements PreparedStatementCreator {
private PreparedStatement statement;
public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
if (statement != null)
return statement;
PreparedStatement ps = doPreparedStatement(conn);
ProxyFactory pf = new ProxyFactory(ps);
MethodInterceptor closeMethodInterceptor = new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
return null; // don't close statement
}
};
NameMatchMethodPointcutAdvisor closeAdvisor = new NameMatchMethodPointcutAdvisor();
closeAdvisor.setMappedName("close");
closeAdvisor.setAdvice(closeMethodInterceptor);
pf.addAdvisor(closeAdvisor);
statement = (PreparedStatement) pf.getProxy();
return statement;
}
public abstract PreparedStatement doPreparedStatement(Connection conn) throws SQLException;
public void close() {
try {
PreparedStatement ps = (PreparedStatement) ((Advised) statement).getTargetSource().getTarget();
ps.close();
} catch (Exception e) {
// handle exception
}
}
}
关于spring - 使用 Spring JDBC 的 PreparedStatementCreator 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15113610/
我一直在使用 Databricks JDBC 驱动程序版本 2.6.22,并尝试升级到 2.6.27。但是,升级后我收到消息说我的 JDBC URL 在尝试连接时无效。这些 JDBC URL 与旧版本
似乎JDBC Spec没有详细说明数据源连接池中alive or idle connections的准确含义。它只是具体实现吗? DBCP2如何或 HikariCP实际检查连接状态? 下面没有事件事务
在“XPages 扩展库”一书中,第 12 章,第 409 页有一个 JDBC 连接文件的例子: org.apache.derby.jdbc.EmbeddedDriver jdbc:
谁能告诉我 jdbc 是如何工作的?它如何设法与 DBMS 通信?因为 DBMS 可能是用其他编程语言编写的。 最佳答案 与数据库的通信由 JDBC 驱动程序处理,这些驱动程序可以使用各种策略与数据库
我想知道是否有人可以帮助我解决这个问题。我在尝试使用 Spring JDBC 编写代码时遇到了一个问题。当我运行服务器时,我收到了标题中提到的消息。我google了一下,有人说你应该导入ojdbc.j
我只是想运行一个示例 hivejdbc 客户端程序,但它给我一个内存不足的错误。 import java.sql.SQLException; import java.sql.Connection; i
我需要将 Google Spreadsheet 与 JasperReports Server 一起使用,为此我需要一个用于 Google Spreadsheet 的 JDBC 连接器。 我找到了这个
我需要将大量行(最多 100,000 行)插入到 6 个不同的 DB2 表中。我正在使用 Java JDBC 来完成它。我想在单个数据库事务中完成所有操作,以便在遇到任何问题时可以回滚整个操作。在某处
再次为自己是 Jmeter 新手道歉——我对 JDBC 请求有点困惑——我在过去的 3 个小时里浏览了这个网站上的帖子——但我找不到任何相关的东西(除非我我错过了一些东西)。 我的环境:Jmeter
我们正在创建一个带有 MySQL 后端的 XPages 应用程序。应用程序将被多个客户使用。每个都有自己的 NSF 数据库和相应的 MySQL 数据库。每个客户都有自己的 MySQL 用户名。我们正在
昨天我遇到了一个大问题。在我当前的项目中,我使用 Oracle 的 JDBC 的 ojdbc6 实现进行连接,但我还需要处理例如 oracle 8 数据库,这对于这个 JAR 是完全不可能的。 你会说
这个问题在这里已经有了答案: Closing JDBC Connections in Pool (3 个答案) 关闭 2 年前。 假设我有以下代码 DataSource source = (Data
我有 Informix 数据库,时间戳字段定义为 YEAR TO SECOND。 当我使用 JDBC rs.getString(column) 显示此字段时,它使用带毫秒的格式,因此此字段如下所示:
看完本教程之后; https://www.youtube.com/watch?v=ZnI_rlrei1s 我正在尝试使用logstash和jdbc获取我的本地主机mysql(使用laravel val
有人给我小费。 { "type": "jdbc", "jdbc": { "driver": "com.microsoft.sqlserver.jdbc.SQLServerDriver"
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我正在尝试从mysql表中将1600万个文档(47gb)索引为elasticsearch索引。我正在使用jparante's elasticsearch jdbc river执行此操作。但是,在创建河
我正在尝试使用JDBC河将我的MySQL数据库复制到我的ElasticSearch索引中。 但是,每当我启动服务器时,与MySQL表的count(*)相比,创建的文档数量就增加了一倍。我通过清空索引并
使用新的logstash jdbc 连接器: https://www.elastic.co/guide/en/logstash/current/plugins-inputs-jdbc.html后续的
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我是一名优秀的程序员,十分优秀!