Skip to content

Commit 9c839db

Browse files
committed
included jdk 1.7 try-with-resources sample
1 parent 30a249a commit 9c839db

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/site/xdoc/java-api.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,22 @@ void rollback(boolean force)</source>
252252
<p>The most important thing you must ensure is that you close any sessions that you open. The best way to ensure this is to use the following unit of work pattern:</p>
253253
<source>SqlSession session = sqlSessionFactory.openSession();
254254
try {
255-
// following 3 lines pseudocod for "doing some work"
255+
// following 3 lines pseudocode for "doing some work"
256256
session.insert(...);
257257
session.update(...);
258258
session.delete(...);
259259
session.commit();
260260
} finally {
261261
session.close();
262+
}</source>
263+
<p>Or, If you are using jdk 1.7+ and MyBatis 3.2+, you can use the try-with-resources statement:</p>
264+
<source>
265+
try (SqlSession session = sqlSessionFactory.openSession()) {
266+
// following 3 lines pseudocode for "doing some work"
267+
session.insert(...);
268+
session.update(...);
269+
session.delete(...);
270+
session.commit();
262271
}</source>
263272
<p><span class="label important">NOTE</span> Just like SqlSessionFactory, you can get the instance of Configuration that the SqlSession is using by calling the getConfiguration() method.</p>
264273
<source>Configuration getConfiguration()</source>

0 commit comments

Comments
 (0)