@@ -71,7 +71,6 @@ public static void setUp() throws Exception {
71
71
Environment environment = new Environment ("development" , new JdbcTransactionFactory (), new UnpooledDataSource (
72
72
"org.postgresql.Driver" , url , null ));
73
73
configuration .setEnvironment (environment );
74
- configuration .setUseGeneratedKeys (true );
75
74
configuration .addMapper (Mapper .class );
76
75
sqlSessionFactory = new SqlSessionFactoryBuilder ().build (configuration );
77
76
@@ -154,58 +153,42 @@ public void shouldGetResultNullFromCallableStatement() throws Exception {
154
153
}
155
154
156
155
@ Test
157
- public void shouldReturnXmlAsString () throws Exception {
158
- SqlSession session = sqlSessionFactory .openSession ();
159
- try {
156
+ public void shouldReturnXmlAsString () {
157
+ try (SqlSession session = sqlSessionFactory .openSession ()) {
160
158
Mapper mapper = session .getMapper (Mapper .class );
161
159
XmlBean bean = mapper .select (1 );
162
160
assertEquals ("<title>XML data</title>" ,
163
161
bean .getContent ());
164
- } finally {
165
- session .close ();
166
162
}
167
163
}
168
164
169
165
@ Test
170
- public void shouldReturnNull () throws Exception {
171
- SqlSession session = sqlSessionFactory .openSession ();
172
- try {
166
+ public void shouldReturnNull () {
167
+ try (SqlSession session = sqlSessionFactory .openSession ()) {
173
168
Mapper mapper = session .getMapper (Mapper .class );
174
169
XmlBean bean = mapper .select (2 );
175
170
assertNull (bean .getContent ());
176
- } finally {
177
- session .close ();
178
171
}
179
172
}
180
173
181
174
@ Test
182
- public void shouldInsertXmlString () throws Exception {
175
+ public void shouldInsertXmlString () {
183
176
final Integer id = 100 ;
184
177
final String content = "<books><book><title>Save XML</title></book><book><title>Get XML</title></book></books>" ;
185
178
// Insert
186
- {
187
- SqlSession session = sqlSessionFactory .openSession ();
188
- try {
189
- Mapper mapper = session .getMapper (Mapper .class );
190
- XmlBean bean = new XmlBean ();
191
- bean .setId (id );
192
- bean .setContent (content );
193
- mapper .insert (bean );
194
- session .commit ();
195
- } finally {
196
- session .close ();
197
- }
179
+ try (SqlSession session = sqlSessionFactory .openSession ()) {
180
+ Mapper mapper = session .getMapper (Mapper .class );
181
+ XmlBean bean = new XmlBean ();
182
+ bean .setId (id );
183
+ bean .setContent (content );
184
+ mapper .insert (bean );
185
+ session .commit ();
198
186
}
199
187
// Select to verify
200
- {
201
- SqlSession session = sqlSessionFactory .openSession ();
202
- try {
203
- Mapper mapper = session .getMapper (Mapper .class );
204
- XmlBean bean = mapper .select (id );
205
- assertEquals (content , bean .getContent ());
206
- } finally {
207
- session .close ();
208
- }
188
+ try (SqlSession session = sqlSessionFactory .openSession ()) {
189
+ Mapper mapper = session .getMapper (Mapper .class );
190
+ XmlBean bean = mapper .select (id );
191
+ assertEquals (content , bean .getContent ());
209
192
}
210
193
}
211
194
0 commit comments