- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在完成数据库查询后关闭我的连接。问题是我正在使用准备好的语句,因为它们是预编译的,所以更安全。我想关闭数据库连接,以便稍后可以将其重新用于另一个查询。
文档内容如下:
/**
* Create a statement in this connection. Allows implementations to use
* PreparedStatements. The JdbcTemplate will close the created statement.
* @param con the connection used to create statement
* @return a prepared statement
* @throws SQLException there is no need to catch SQLExceptions
* that may be thrown in the implementation of this method.
* The JdbcTemplate class will handle them.
*/
PreparedStatement createPreparedStatement(Connection con) throws SQLException;
我遇到了池中没有可用连接的情况。
我有以下内容,但它抛出一个异常,表示语句关闭后不允许进行任何操作。
private IRespondent InsertRespondentToken(IRespondent respondent) {
try{
final String insertRespondent = "insert into respondents_token (SynchroID,TerminalID,QuestionnaireID,ProjectID,Token) values (?,?,?,?,?)";
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(
(Connection con) -> {
try{
PreparedStatement pst = con.prepareStatement(insertRespondent, new String[] {"ID"});
pst.setInt(1, respondent.getSynchroId());
pst.setInt(2, respondent.getTerminalId());
pst.setInt(3, respondent.getQuestionnaireId());
pst.setInt(4,respondent.getProjectId());
respondent.setToken(GenerateUniqueId.getIdentifier());
pst.setString(5,respondent.getToken());
return pst;
}
catch (SQLException e)
{
log.error("Prepared statement failed! Read the stack!",e);
}
finally {
con.close(); // IS thsi right. Or there is another way of doing it.
}
return null;
}
,keyHolder);
}catch(NullPointerException ex){
log.error("Error during end element parsing.", ex);
if (respondent.getId() != -1)
deleteRespondent(respondent.getId());
return null;
}
return respondent;
}
最佳答案
您将关闭连接,并扩展您创建的准备好的语句。因此,当 JdbcTemplate 继续执行该语句时,连接(和语句)已经关闭。您需要删除代码周围的 try-catch-finally
来关闭连接(并吞掉任何异常)。
另请参阅 Spring 文档中 Retrieving Auto-generated Keys 部分的示例。您所需要做的就是准备语句并填充其参数。
看待这个问题的另一种方式是使用资源管理的基本“规则”:您只关闭您创建的内容(除非另有说明)。在这种情况下,您没有创建连接,因此您不负责关闭它。
关于java - JDBCTemplate 执行查询后关闭准备好的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58944432/
应用程序上下文中的一些bean的依赖关系形成一个循环:当我使用时@Autowired私有(private) JdbcTemplate jdbcTemplate;不同类(class) 错误提示: ***
我做一个查询: jdbcTemplate.query(sqlQueryForGetNodes, new Object[]{treeId, versionId}, rs -> { NodeTy
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 3 年前。 Improve this ques
根据 Spring documentation ,Spring JdbcTemplate的使用步骤如下:
我做了这个简单的应用程序来连接到 MySQL 数据库,我收到了这个错误: org.springframework.jdbc.core.JdbcTemplate 在我的配置中 (com.kubamadr
我遵循了SpringBoot框架中的示例,该框架从这里开始,当我尝试添加Spring Bean时得到下一个错误。Pom.xml。我尝试插入到BD中,BD没有改变,应用程序本身可以工作,但没有连接数据库
我遵循了SpringBoot框架中的示例,该框架从这里开始,当我尝试添加Spring Bean时得到下一个错误。Pom.xml。我尝试插入到BD中,BD没有改变,应用程序本身可以工作,但没有连接数据库
我正在学习 Spring Boot 和 jdbcTemplate 的组合以进行一些基本的 crud 操作,并试图更好地理解我应该选择哪种更新方法。 我理解以下两种类方法(改编自 this post)将
如何使用行映射器向表中插入数据? 我正在尝试: Employee user1 = jtemplate.queryForObject("INSERT INTO employee(id, name,sal
我最近切换到 Spring Framework 而不是手动处理 JDBC,这主要是一个很好的过渡。但是,一个程序开始出现奇怪的问题:如果数据库速度很慢,则在调用 getJdbcTemplate().u
命令(Command)模式是指将请求封装成为一个对象,使发出请求和执行请求的责任分割开,方便将命令对象进行存储、传递、调用、增加与管理。 也就是将发送者、接收者和调用命令封装成独立的对象,来供客户端调
我有一个 spring 应用程序,它的主页会触发多个 ajax 调用,这些调用又从数据库中获取数据并返回。数据库已配置连接池,minPoolSize 为 50,maxPoolSize 为 100。 现
有人可以指出我以下 Spring Jdbc 模板代码中的任何错误吗? 当我点击删除时,记录没有被删除,也没有显示错误。 public void delete(String id) { logg
我正在使用 spring JDBCTemplate。 我有一个场景,其中需要传递到查询函数中的参数是条件/可选的。例如,我有以下代码: List result = jdbcTemplate.query
在项目中我在Hibernate和Spring jdbctemplate中是混合使用的。我添加了乐观锁定。 Hibernate 非常适合版本控制,但现在我必须转换所有这些 jdbctemplate 代码
我已经为我的INSERT查询建立了一个DAO。码: DAO public class EmployeeDao { JdbcTemplate template; public void
我有一个sql查询。 String sql = "SELECT ? FROM Users WHERE Lastname=?"; 我使用 JDBCTemplate 中的 queryForList 方法
我正在尝试将嵌套查询与 JdbcTemplate 一起使用,但发现了问题,在我看来,它不支持嵌套查询..我是对的吗?或者我需要改变什么? 所以,我调用 getJdbcTemplate().query
这个问题是几年前提出的,但答案对我来说不起作用。我已将建议的注释添加到配置和 dao 中。我确信模板实际上正在连接到数据库,因为当我的列太小时,我收到了适当的错误。更新调用正在执行单行插入,并且毫无异
JdbcTemplate 对象和 SimpleJdbcTemplate 之间有什么区别? 最佳答案 截至Spring 3.1 SimpleJdbcTemplate已弃用,SimpleJdbcTempl
我是一名优秀的程序员,十分优秀!