Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #37131
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Not closing a SQL(ite3) connection |
| Date | 2017-08-11 10:26 +0200 |
| Organization | Decebal Computing |
| Message-ID | <874lte1pdj.fsf@munus.decebal.nl> (permalink) |
| References | <87a839um2q.fsf@munus.decebal.nl> <omg0la$1qe3$1@gioia.aioe.org> |
Arne Vajhøj <arne@vajhoej.dk> writes:
> On 8/9/2017 5:28 AM, Cecil Westerhof wrote:
>> Normally I always close a connection when finished. But I now wrote my
>> own class SQLite3 to handle things in this class. It has its own
>> connection. Can there be a problem when a connection is not closed, or
>> is that not something to worry myself about?
>>
>> Would it be a good idea to write a function to close the database
>> connection?
>
> If the class implements the JDBC standard interfaces it will need
> a close method to compile.
That is a bit to much for me. ;-) It is just a wrapper around the
SQLite JDBC class.
> But even if it does not then I will recommend adding a close
> method *and* implement AutoCloseable.
I made the class AutoClosable. I use it during the whole run, but
maybe someone else will use it in a try with resources.
> It is the right way to handle database connections.
>
> In some contexts like SE app with embedded DB it is not likely
> to make a difference, but it is still a good to do things the right
> way. To maintain good habits. And to prevent disasters if the
> context suddenly change (that sometimes happens).
Completely my idea. I am often told: “That is not necessary.”, but in
my experience there is much more harm done by ‘not doing what is not
necessary’ as by ‘doing more as necessary’.
I have a strange problem. I made a class SQLite3ResultSet. There is a
lot of stuff to be removed, but that is just to find out if it is
doing what I think it is doing.
In this class I have:
private ResultSet resultSet = null;
private Statement stmt = null;
The constructor is:
public SQLite3ResultSet(Connection conn, String query) throws SQLite3Exception {
System.out.println("SQLite3ResultSet: constructor.");
try {
stmt = conn.createStatement();
resultSet = stmt.executeQuery(query);
} catch (SQLException e1) {
String errorMsg = "";
if (stmt != null) {
try {
stmt.close();
stmt = null;
} catch (SQLException e2) {
errorMsg += "Could not close Statement.";
}
}
throw new SQLite3Exception(errorMsg +
"Could not create a resultSet.");
}
}
And the close method is (for now, because I want to test it):
@Override
public void close() throws SQLite3Exception {
System.out.println("SQLite3ResultSet: closing open resources.");
String errorMsg = "";
try {
stmt.close();
resultSet.next();
resultSet.close();
} catch (SQLException e) {
errorMsg += "Could not close ResultSet.\n";
System.out.println(errorMsg);
}
System.out.println("Closed resultSet.");
try {
stmt.close();
} catch (SQLException e) {
errorMsg += "Could not close Statement belonging to ResultSet.\n";
System.out.println(errorMsg);
}
System.out.println("closed stmt");
System.out.println("SQLite3ResultSet: closed open resources.");
if (!errorMsg.isEmpty()) {
throw new SQLite3Exception(errorMsg);
}
}
In the first try I close the stmt and then I do a resultSet.next(). I
would expect an exception, because I closed the stmt, but I do not get
an exception. Why not?
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 11:28 +0200
Re: Not closing a SQL(ite3) connection Arne Vajhøj <arne@vajhoej.dk> - 2017-08-09 18:00 -0400
Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-11 10:26 +0200
Re: Not closing a SQL(ite3) connection Arne Vajhøj <arne@vajhoej.dk> - 2017-08-11 20:27 -0400
Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-12 10:00 +0200
Re: Not closing a SQL(ite3) connection Andreas Leitgeb <avl@logic.at> - 2017-08-12 08:51 +0000
Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-12 11:05 +0200
Re: Not closing a SQL(ite3) connection Arne Vajhøj <arne@vajhoej.dk> - 2017-08-12 09:52 -0400
Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-12 16:26 +0200
Re: Not closing a SQL(ite3) connection Arne Vajhøj <arne@vajhoej.dk> - 2017-08-12 21:43 -0400
Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-15 08:19 +0200
Re: Not closing a SQL(ite3) connection lewbloch@gmail.com - 2017-08-12 10:14 -0700
Re: Not closing a SQL(ite3) connection Andreas Leitgeb <avl@logic.at> - 2017-08-12 09:33 +0000
Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-12 12:57 +0200
Re: Not closing a SQL(ite3) connection Andreas Leitgeb <avl@logic.at> - 2017-08-12 13:27 +0000
Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-12 16:37 +0200
Re: Not closing a SQL(ite3) connection Arne Vajhøj <arne@vajhoej.dk> - 2017-08-12 09:55 -0400
Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-12 16:44 +0200
Re: Not closing a SQL(ite3) connection Arne Vajhøj <arne@vajhoej.dk> - 2017-08-12 21:45 -0400
Re: Not closing a SQL(ite3) connection Martin Gregorie <martin@address-in-sig.invalid> - 2017-08-12 21:04 +0000
Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-13 00:30 +0200
Re: Not closing a SQL(ite3) connection Martin Gregorie <martin@address-in-sig.invalid> - 2017-08-13 11:34 +0000
Re: Not closing a SQL(ite3) connection Cecil Westerhof <Cecil@decebal.nl> - 2017-08-13 15:27 +0200
Re: Not closing a SQL(ite3) connection Andreas Leitgeb <avl@logic.at> - 2017-08-12 13:06 +0000
csiph-web