Is there anything wrong in closing my connection resources like this? I seem to still have idle connections in postgres running.
public void doSomething(){
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
con = getConnection();
ps = con.prepareStatement("sql here");
....
rs = ps.executeQuery();
...
} catch(Exceptions stuff){
} finally {
closeAll(con,ps,rs);
}
}
public void closeAll(Connection con, PreparedStatement ps, ResultSet rs){
try{
if(rs != null){
rs.close();
}
if(ps != null){
ps.close();
}
if(con != null){
con.close();
}
} catch(SQLException e){
....
}
}
Practice the Best Practice
Yes, their is a problem in closing connection the way you have closed.
Suppose, an exception occurred while closing ResultSet object the rest things would not be closed
Second, suppose if everything goes fine, still you are holding other connection (etc) you are not using, it adds to the burden to the JVM, Database, Memory manager etc
It is recommended to use " try(){} with resource " feature available in JAVA 7 or if you are using JAVA 6 close them when it is no longer needed.
Related
I am using h2 in memory database for testing. I have a situation where nested database calls will happen. Using h2 the connection is getting closed on the nested call, even if the objects are different.
ClassA:
Public class TestDb {
Public String getById(String id) {
try {
Connection conn= getConnection();
PreparedStatement ps = conn.prepareStatement(“query”);
ResultEntity rs = ps.executeQuery();
getOthersById(rs.get(id));
//here rs is closed and seering error as: object is closed
rs.get(2);
}Catch(Exception ex) {
//handle exceptions
}finally {
// close resultset
//close ps
// close conn
}
}
public int getOthersById(String id) {
try {
//get new connection
Connection conn= getConnection();
PreparedStatement ps = conn.prepareStatement(“query”);
ResultEntity rs = ps.executeQuery();
rs.get(id);
}Catch(Exception ex) {
//handle exceptions
}finally {
// close resultset
//close ps
// close conn
}
}
}
The problem in h2 is after the closing new connection and result set in getOthersById method, resultSet in getById is closed.
I am creating the h2 as below:
jdbc:h2:mem:test;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;
This is my code at the moment. I have or had several problems:
Database is busy - error
SQL error or missing database
Resultset closed
Which code structure should I use if I want to prevent both of these errors?
Do I need to connect in the try block? Where do I need to close the connection? Where do I need to close the preparedStatement?
I was not able to find a solution by connecting the information of different pages by myself until now.
public boolean checkForUser(String username){
try(Connection con = this.connect();
PreparedStatement pstmt = createPreparedStatementRegistrate(conn, username);
ResultSet rs= pstmt.executeQuery();)
{
if (rueck.next()){
pstmt.close();
rs.close();
con.close();
//do some stuff
}
}catch(SQLException e){
e.printStackTrace();
}
return null;
}
private PreparedStatement createPreparedStatementRegistrate(Connection conn, String username) throws SQLException {
String sql = "SELECT * FROM users WHERE username = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
return pstmt;
}
private Connection connect() {
String url = "jdbc:sqlite:user.db";
Connection con = null;
try {
con = DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
java.sql.SQLException: ResultSet closed
at org.sqlite.core.CoreResultSet.checkOpen(CoreResultSet.java:69)
at org.sqlite.jdbc3.JDBC3ResultSet.findColumn(JDBC3ResultSet.java:38)
at org.sqlite.jdbc3.JDBC3ResultSet.getString(JDBC3ResultSet.java:443)
at zugriffsverwaltung.Zugriffsverwaltung.loeschen(Zugriffsverwaltung.java:215)
at zugriffsverwaltung.Zugriffsverwaltung.<init>(Zugriffsverwaltung.java:37)
at main.Server.<init>(Server.java:29)
at main.Server.main(Server.java:46)
org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (Connection is closed)
at org.sqlite.core.DB.newSQLException(DB.java:909)
at org.sqlite.core.CoreStatement.internalClose(CoreStatement.java:115)
at org.sqlite.jdbc3.JDBC3Statement.close(JDBC3Statement.java:35)
at org.sqlite.jdbc4.JDBC4Statement.close(JDBC4Statement.java:27)
at zugriffsverwaltung.Zugriffsverwaltung.loeschen(Zugriffsverwaltung.java:238)
at zugriffsverwaltung.Zugriffsverwaltung.<init>(Zugriffsverwaltung.java:37)
at main.Server.<init>(Server.java:29)
at main.Server.main(Server.java:46)
The problem is that you are closing the result set, statement and connection before you do things with the result set. This is not allowed: a result set needs to be open when you retrieve values using getString (not shown in your code, but present in your stack trace).
As you are using try-with-resource which will close the resource on exit, it is even unnecessary to explicitly close. It also looks like the SQLite driver does not meet the JDBC requirements that closing an already closed resource should be a no-op, as it is throwing an exception that the connection is closed when closing the statement (likely by the try-with-resources).
You need to change your code to
public boolean checkForUser(String username){
try(Connection con = this.connect();
PreparedStatement pstmt = createPreparedStatementRegistrate(conn, username);
ResultSet rs= pstmt.executeQuery();)
{
if (rueck.next()){
//do some stuff
}
}catch(SQLException e){
e.printStackTrace();
}
return null;
}
That is, remove the lines
pstmt.close();
rs.close();
con.close();
I'm curious about best practices when it comes to database interaction. I've been using a pattern that I believe handles making sure all of the appropriate objects are closed when I'm done with them. However, a coworker recently refactored my code with a comment along the lines of, "making sure we always close database objects". I need to know if one pattern is "better" than the other for some reason. Is the pattern that I've been using wrong somehow? Does one pattern have advantages over the other?
The pattern that I've been following:
public void doStuff() {
try {
final Connection connection = this.getConnection();
try {
final PreparedStatement ps = connection.prepareStatement("SELECT COLA, COLB FROM TBL WHERE COLC = ?");
try {
ps.setString(1, "asdf");
final ResultSet rs = ps.executeQuery();
try {
// get data from rs
} finally {
rs.close();
}
} finally {
ps.close();
}
} finally {
connection.close();
}
} catch (SQLException e) {
// do something with the error
}
}
The pattern that my coworker modifed my code to:
public void doStuff() {
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
connection = this.getConnection();
ps = connection.prepareStatement("SELECT COLA, COLB FROM TBL WHERE COLC = ?");
ps.setString(1, "asdf");
rs = ps.executeQuery();
// get data from rs
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// do something with the error
}
}
if (ps!= null) {
try {
ps.close();
} catch (SQLException e) {
// do something with the error
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// do something with the error
}
}
}
}
If you're using Java 6 or prior, then use the latter because it's easier to read and maintain. Note that the latter can be improved with some refactoring to handle the cumbersome try-catch for every call to close method.
If you're using Java 7 or higher, then use try-with-resources:
try (Connection con = ...;
PreparedStatement pstmt = ...) {
pstmt.setXyz(...);
ResultSet rs = pstmt.executeQuery();
//read data from resultset
//and then close it
rs.close();
} catch (Exception e) {
//handle the exception properly...
}
In case you want to make sure about closing the ResultSet, you may use a nested try-with-resources:
try (Connection con = ...;
PreparedStatement pstmt = ...) {
pstmt.setXyz(...);
try(ResultSet rs = pstmt.executeQuery()) {
//read data from resultset
}
} catch (Exception e) {
//handle the exception properly...
}
The latter is easier to read; deep nesting is hard to reason about.
I prefer safe wrappers around closeables, e.g., they do nothing if the closeable is null. This also makes the mainline code easier to read.
Luigi's answer makes the most sense from Java 7 on, of course.
It's often simpler and cleaner to abstract the closure of your database resources to a dedicated manager object, which will contain any NPE's and such that might be thrown.
A pretty well written one exists as part of the open source project, OpenFire:
https://github.com/igniterealtime/Openfire/blob/master/src/java/org/jivesoftware/database/DbConnectionManager.java#L243
Sample helper method from this DbConnectionManager:
public static void closeResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
}
catch (SQLException e) {
Log.error(e.getMessage(), e);
}
}
}
So in your finally block you just pass your resources back into your manager, and it handles the ugly logic to test for nulls and catch exceptions, etc.
Like:
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DbConnectionManager.getConnection();
ps = con.prepareStatement(yourStatement);
rs = ps.executeQuery();
if (rs != null) {
while (rs.next()) {
// do stuff
}
}
} catch (SQLException e) {
LOG.error(e.getMessage(), e);
} finally {
DbConnectionManager.closeConnection(rs, ps, con);
}
Simulating Go's defer statement :D
try(Defer defer = new Defer())
{
Connection connection = ...;
defer.add( connection::close );
....
Path tmpFile = ...;
defer.add( ()->Files.delete(tmpFile) );
....
} // Defer.close() => executing registered actions, from last to first
How Defer is implemented is left as an exercise to readers:)
I am developing a dynamic web project on eclipse.
Below is an example of connecting MySQL using DataSource.
Is it the correct way? I mean is it normal to get connection in a Servlet?
Moreover, I find this way to get/close connection is tedious because I need to write exact same part of codes every time when I want to get/close connection. I think there should be a better way. Could someone give me some suggestions?
Thank you!
#WebServlet(name="HelloUser", urlPatterns={"/hellouser"})
public class HelloUserServlet extends HttpServlet{
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
DataSource ds = MyDataSourceFactory.getMySQLDataSource();
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = ds.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery(...);
...
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(con != null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
Starting from Java 7 you can use try-with-resource( JDBC api is updated to implement Autocloseable) .
The try-with-resources statement is a try statement that declares one
or more resources. A resource is an object that must be closed after
the program is finished with it
E.g.
try (Connection con = ds.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(...)) {...}
When using a PreparedStatement in JDBC, should I close the PreparedStatement first or the Connection first? I just saw a code sample in which the Connection is closed first, but it seems to me more logical to close the PreparedStatement first.
Is there a standard, accepted way to do this? Does it matter? Does closing the Connection also cause the PreparedStatement to be closed, since the PreparedStatement is directly related to the Connection object?
The statement. I would expect you to close (in order)
the result set
the statement
the connection
(and check for nulls along the way!)
i.e. close in reverse order to the opening sequence.
If you use Spring JdbcTemplate (or similar) then that will look after this for you. Alternatively you can use Apache Commons DbUtils and DbUtils.close() or DbUtils.closeQuietly().
The following procedures should be done (in order)
The ResultSet
The PreparedStatement
The Connection.
Also, it's advisable to close all JDBC related objects in the finally close to guarantee closure.
//Do the following when dealing with JDBC. This is how I've implemented my JDBC transactions through DAO....
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = ....
ps = conn.prepareStatement(...);
//Populate PreparedStatement
rs = ps.executeQuery();
} catch (/*All relevant exceptions such as SQLException*/Exception e) {
logger.error("Damn, stupid exception: " , e);
} finally {
if (rs != null) {
try {
rs.close();
rs = null;
} catch (SQLException e) {
logger.error(e.getMessage(), e.fillInStackTrace());
}
}
if (ps != null) {
try {
ps.close();
ps = null;
} catch (SQLException e) {
logger.error(e.getMessage(), e.fillInStackTrace());
}
}
try {
if (conn!= null && !conn.isClosed()){
if (!conn.getAutoCommit()) {
conn.commit();
conn.setAutoCommit(true);
}
conn.close();
conn= null;
}
} catch (SQLException sqle) {
logger.error(sqle.getMessage(), sqle.fillInStackTrace());
}
}
You can see I've checked if my objects are null and for connection, check first if the connection is not autocommited. Many people fail to check it and realise that the transaction hasn't been committed to DB.