resource-management

Where to close java PreparedStatements and ResultSets?

守給你的承諾、 提交于 2019-11-27 00:56:51
Consider the code: PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.createStatement(myQueryString); rs = ps.executeQuery(); // process the results... } catch (java.sql.SQLException e) { log.error("an error!", e); throw new MyAppException("I'm sorry. Your query did not work."); } finally { ps.close(); rs.close(); } The above does not compile, because both PreparedStatement.close() and ResultSet.close() throw a java.sql.SQLException . So do I add a try/catch block to the finally clause? Or move the close statements into the try clause? Or just not bother calling close? For file

RAII in Java… is resource disposal always so ugly?

不想你离开。 提交于 2019-11-26 20:28:10
问题 I just played with Java file system API, and came down with the following function, used to copy binary files. The original source came from the Web, but I added try/catch/finally clauses to be sure that, should something wrong happen, the Buffer Streams would be closed (and thus, my OS ressources freed) before quiting the function. I trimmed down the function to show the pattern: public static void copyFile(FileOutputStream oDStream, FileInputStream oSStream) throw etc... {

What does “opening a connection” actually mean?

≡放荡痞女 提交于 2019-11-26 16:36:36
问题 I was trying to explain to someone why database connections implement IDisposable, when I realized I don't really know what "opening a connection" actually mean. So my question is - What does c# practically do when it opens a connection? Thank you. 回答1: There are actually two classes involved in implementing a connection (actually more, but I'm simplifying). One of these is the IDbConnection implementation ( SQLConnection , NpgsqlConnection , OracleConnection , etc.) that you use in your code

Where to close java PreparedStatements and ResultSets?

本秂侑毒 提交于 2019-11-26 09:27:07
问题 Consider the code: PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.createStatement(myQueryString); rs = ps.executeQuery(); // process the results... } catch (java.sql.SQLException e) { log.error(\"an error!\", e); throw new MyAppException(\"I\'m sorry. Your query did not work.\"); } finally { ps.close(); rs.close(); } The above does not compile, because both PreparedStatement.close() and ResultSet.close() throw a java.sql.SQLException . So do I add a try/catch block to the

Understanding the meaning of the term and the concept - RAII (Resource Acquisition is Initialization)

让人想犯罪 __ 提交于 2019-11-26 00:39:41
问题 Could you C++ developers please give us a good description of what RAII is, why it is important, and whether or not it might have any relevance to other languages? I do know a little bit. I believe it stands for \"Resource Acquisition is Initialization\". However, that name doesn\'t jive with my (possibly incorrect) understanding of what RAII is: I get the impression that RAII is a way of initializing objects on the stack such that, when those variables go out of scope, the destructors will