简体   繁体   English

JDBC连接关闭概念

[英]JDBC Connection close conecpt

I have created connection's object and stored in the hashMap. 我创建了连接的对象并存储在hashMap中。 While executing query on the connection object I retrieve the object from the hashMap. 在对连接对象执行查询时,我从hashMap检索对象。 In finally block I close the connection. 最后,我关闭连接。 However, when we come for the second time I again retrieve the connection's object from the hashMap. 但是,当我们第二次来时,我再次从hashMap中检索连接的对象。 This time while executing the query I get closed connection exception. 这次执行查询时,我得到了封闭的连接异常。 If I remove conn.close() from finally, it works. 如果我从最后删除conn.close(),它将起作用。 what is the reason behind this? 这背后的原因是什么? Moreover, when i print the connection's object I get the object. 此外,当我打印连接的对象时,会得到该对象。

If you are re-using the same Connection object for multiple queries, you shouldn't close the connection after each time you use it, since closing it means the second query will fail. 如果您将同一个Connection对象重新用于多个查询,则不应在每次使用连接后都将其关闭,因为关闭它意味着第二个查询将失败。

Therefore, you should either create a new open Connection for each query, or keep the cached Connection open (ie don't close it in the finally block). 因此,您应该为每个查询创建一个新的打开的Connection ,或者使缓存的Connection保持打开状态(即,不要在finally块中将其关闭)。

I think you have to go for a connection pool implementation instead of a simple Map of connections. 我认为您必须使用连接池实现,而不是简单的连接图。 Here when you close a connection after each transaction, the life cycle of the connection ends, and you can not reuse it from the map. 在这里,当您在每个事务之后关闭连接时,连接的生命周期将结束,并且您将无法从映射中重用它。

Think of a connection pool which can get you an already opened connection, and return/release the connection back to the pool once your usage is done. 想想一个连接池,它可以使您已经打开的连接,并在使用完成后将连接返回/释放回该池。

I think you need to move to some implementation of the connection pool. 我认为您需要转向连接池的某些实现。

PS It would be helpful to see your code PS看到您的代码会很有帮助

You don't have to close Connection after each sql statement. 您不必在每个sql语句后关闭Connection。 Newly created Connection is in auto-commit mode : By default, new connections are in auto-commit mode. 新创建的连接处于自动提交模式:默认情况下,新连接处于自动提交模式。 From Connection.setAutoCommit doc: Connection.setAutoCommit文档:

The commit occurs when the statement completes. 语句完成时,将进行提交。 The time when the statement completes depends on the type of SQL Statement: •For DML statements, such as Insert, Update or Delete, and DDL statements, the statement is complete as soon as it has finished executing. 语句完成的时间取决于SQL语句的类型:•对于DML语句,例如Insert,Update或Delete和DDL语句,该语句在完成执行后立即完成。 •For Select statements, the statement is complete when the associated result set is closed. •对于Select语句,当关联的结果集关闭时,该语句完成。 •For CallableStatement objects or for statements that return multiple results, the statement is complete when all of the associated result sets have been closed, and all update counts and output parameters have been retrieved. •对于CallableStatement对象或返回多个结果的语句,当所有关联的结果集都已关闭并且所有更新计数和输出参数均已获取时,该语句将完成。

But if you set auto-commit to false you have to call Connection.commit method to update database. 但是,如果将auto-commit设置为false,则必须调用Connection.commit方法来更新数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM