6
6
look like this:
7
7
</para >
8
8
9
- <programlisting ><![CDATA[ ISession session = sessionFactory.OpenSession();
10
- ITransaction tx = session.BeginTransaction();
11
- for ( int i=0; i<100000; i++ ) {
12
- Customer customer = new Customer(.....);
13
- session.Save(customer);
14
- }
15
- tx.Commit();
16
- session.Close();]]> </programlisting >
9
+ <programlisting ><![CDATA[ using (ISession session = sessionFactory.OpenSession())
10
+ using (ITransaction tx = session.BeginTransaction())
11
+ {
12
+ for (int i = 0; i < 100000; i++)
13
+ {
14
+ Customer customer = new Customer(.....);
15
+ session.Save(customer);
16
+ }
17
+ tx.Commit();
18
+ }]]> </programlisting >
17
19
18
20
<para >
19
21
This would fall over with an <literal >OutOfMemoryException</literal > somewhere
@@ -56,21 +58,24 @@ session.Close();]]></programlisting>
56
58
the first-level cache.
57
59
</para >
58
60
59
- <programlisting ><![CDATA[ ISession session = sessionFactory.openSession();
60
- ITransaction tx = session.BeginTransaction();
61
-
62
- for ( int i=0; i<100000; i++ ) {
63
- Customer customer = new Customer(.....);
64
- session.Save(customer);
65
- if ( i % 20 == 0 ) { //20, same as the ADO batch size
66
- //flush a batch of inserts and release memory:
67
- session.Flush();
68
- session.Clear();
61
+ <programlisting ><![CDATA[ using (ISession session = sessionFactory.openSession())
62
+ using (ITransaction tx = session.BeginTransaction())
63
+ {
64
+ for (int i = 0; i < 100000; i++)
65
+ {
66
+ Customer customer = new Customer(.....);
67
+ session.Save(customer);
68
+ // 20, same as the ADO batch size
69
+ if (i % 20 == 0)
70
+ {
71
+ // flush a batch of inserts and release memory:
72
+ session.Flush();
73
+ session.Clear();
74
+ }
69
75
}
70
- }
71
-
72
- tx.Commit();
73
- session.Close();]]> </programlisting >
76
+
77
+ tx.Commit();
78
+ }]]> </programlisting >
74
79
75
80
</sect1 >
76
81
@@ -90,20 +95,21 @@ session.Close();]]></programlisting>
90
95
to data aliasing effects, due to the lack of a first-level cache. A stateless
91
96
session is a lower-level abstraction, much closer to the underlying ADO.
92
97
</para >
93
-
94
- <programlisting ><![CDATA[ IStatelessSession session = sessionFactory.OpenStatelessSession();
95
- ITransaction tx = session.BeginTransaction();
96
-
97
- var customers = session.GetNamedQuery("GetCustomers")
98
- .Enumerable<Customer>();
99
- while ( customers.MoveNext() ) {
100
- Customer customer = customers.Current;
101
- customer.updateStuff(...);
102
- session.Update(customer);
103
- }
104
-
105
- tx.Commit();
106
- session.Close();]]> </programlisting >
98
+
99
+ <programlisting ><![CDATA[ using (IStatelessSession session = sessionFactory.OpenStatelessSession())
100
+ using (ITransaction tx = session.BeginTransaction())
101
+ {
102
+ var customers = session.GetNamedQuery("GetCustomers")
103
+ .Enumerable<Customer>();
104
+ while (customers.MoveNext())
105
+ {
106
+ Customer customer = customers.Current;
107
+ customer.updateStuff(...);
108
+ session.Update(customer);
109
+ }
110
+
111
+ tx.Commit();
112
+ }]]> </programlisting >
107
113
108
114
<para >
109
115
Note that in this code example, the <literal >Customer</literal > instances returned
@@ -176,17 +182,17 @@ session.Close();]]></programlisting>
176
182
<literal >IQuery.ExecuteUpdate()</literal > method:
177
183
</para >
178
184
179
- <programlisting ><![CDATA[ ISession session = sessionFactory.OpenSession();
180
- ITransaction tx = session.BeginTransaction();
181
-
182
- string hqlUpdate = "update Customer c set c.name = :newName where c.name = :oldName";
183
- // or string hqlUpdate = "update Customer set name = :newName where name = :oldName";
184
- int updatedEntities = s.CreateQuery( hqlUpdate )
185
- .SetString( "newName", newName )
186
- .SetString( "oldName", oldName )
185
+ <programlisting ><![CDATA[ using ( ISession session = sessionFactory.OpenSession())
186
+ using ( ITransaction tx = session.BeginTransaction())
187
+ {
188
+ string hqlUpdate = "update Customer c set c.name = :newName where c.name = :oldName";
189
+ // or string hqlUpdate = "update Customer set name = :newName where name = :oldName";
190
+ int updatedEntities = s.CreateQuery(hqlUpdate)
191
+ .SetString("newName", newName)
192
+ .SetString("oldName", oldName)
187
193
.ExecuteUpdate();
188
- tx.Commit();
189
- session.Close(); ]]> </programlisting >
194
+ tx.Commit();
195
+ } ]]> </programlisting >
190
196
191
197
<para >
192
198
HQL <literal >UPDATE</literal > statements, by default do not effect the
@@ -198,15 +204,17 @@ session.Close();]]></programlisting>
198
204
This is achieved by adding the <literal >VERSIONED</literal > keyword after the <literal >UPDATE</literal >
199
205
keyword.
200
206
</para >
201
- <programlisting ><![CDATA[ ISession session = sessionFactory.OpenSession();
202
- ITransaction tx = session.BeginTransaction();
203
- string hqlVersionedUpdate = "update versioned Customer set name = :newName where name = :oldName";
204
- int updatedEntities = s.CreateQuery( hqlUpdate )
205
- .SetString( "newName", newName )
206
- .SetString( "oldName", oldName )
207
+
208
+ <programlisting ><![CDATA[ using (ISession session = sessionFactory.OpenSession())
209
+ using (ITransaction tx = session.BeginTransaction())
210
+ {
211
+ string hqlVersionedUpdate = "update versioned Customer set name = :newName where name = :oldName";
212
+ int updatedEntities = s.CreateQuery(hqlUpdate)
213
+ .SetString("newName", newName)
214
+ .SetString("oldName", oldName)
207
215
.ExecuteUpdate();
208
- tx.Commit();
209
- session.Close(); ]]> </programlisting >
216
+ tx.Commit();
217
+ } ]]> </programlisting >
210
218
211
219
<para >
212
220
Note that custom version types (<literal >NHibernate.Usertype.IUserVersionType</literal >)
@@ -218,16 +226,16 @@ session.Close();]]></programlisting>
218
226
method:
219
227
</para >
220
228
221
- <programlisting ><![CDATA[ ISession session = sessionFactory.OpenSession();
222
- ITransaction tx = session.BeginTransaction();
223
-
224
- String hqlDelete = "delete Customer c where c.name = :oldName";
225
- // or String hqlDelete = "delete Customer where name = :oldName";
226
- int deletedEntities = s.CreateQuery( hqlDelete )
227
- .SetString( "oldName", oldName )
229
+ <programlisting ><![CDATA[ using ( ISession session = sessionFactory.OpenSession())
230
+ using ( ITransaction tx = session.BeginTransaction())
231
+ {
232
+ string hqlDelete = "delete Customer c where c.name = :oldName";
233
+ // or String hqlDelete = "delete Customer where name = :oldName";
234
+ int deletedEntities = s.CreateQuery(hqlDelete)
235
+ .SetString("oldName", oldName)
228
236
.ExecuteUpdate();
229
- tx.Commit();
230
- session.Close(); ]]> </programlisting >
237
+ tx.Commit();
238
+ } ]]> </programlisting >
231
239
232
240
<para >
233
241
The <literal >int</literal > value returned by the <literal >IQuery.ExecuteUpdate()</literal >
@@ -302,14 +310,14 @@ session.Close();]]></programlisting>
302
310
An example HQL <literal >INSERT</literal > statement execution:
303
311
</para >
304
312
305
- <programlisting ><![CDATA[ ISession session = sessionFactory.OpenSession();
306
- ITransaction tx = session.BeginTransaction();
307
-
308
- var hqlInsert = "insert into DelinquentAccount (id, name) select c.id, c.name from Customer c where ...";
309
- int createdEntities = s.CreateQuery( hqlInsert )
313
+ <programlisting ><![CDATA[ using ( ISession session = sessionFactory.OpenSession())
314
+ using ( ITransaction tx = session.BeginTransaction())
315
+ {
316
+ var hqlInsert = "insert into DelinquentAccount (id, name) select c.id, c.name from Customer c where ...";
317
+ int createdEntities = s.CreateQuery(hqlInsert)
310
318
.ExecuteUpdate();
311
- tx.Commit();
312
- session.Close(); ]]> </programlisting >
319
+ tx.Commit();
320
+ } ]]> </programlisting >
313
321
314
322
</sect1 >
315
323
0 commit comments