66 look like this:
77 </para >
88
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 >
1719
1820 <para >
1921 This would fall over with an <literal >OutOfMemoryException</literal > somewhere
@@ -56,21 +58,24 @@ session.Close();]]></programlisting>
5658 the first-level cache.
5759 </para >
5860
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+ }
6975 }
70- }
71-
72- tx.Commit();
73- session.Close();]]> </programlisting >
76+
77+ tx.Commit();
78+ }]]> </programlisting >
7479
7580 </sect1 >
7681
@@ -90,20 +95,21 @@ session.Close();]]></programlisting>
9095 to data aliasing effects, due to the lack of a first-level cache. A stateless
9196 session is a lower-level abstraction, much closer to the underlying ADO.
9297 </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 >
107113
108114 <para >
109115 Note that in this code example, the <literal >Customer</literal > instances returned
@@ -176,17 +182,17 @@ session.Close();]]></programlisting>
176182 <literal >IQuery.ExecuteUpdate()</literal > method:
177183 </para >
178184
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)
187193 .ExecuteUpdate();
188- tx.Commit();
189- session.Close(); ]]> </programlisting >
194+ tx.Commit();
195+ } ]]> </programlisting >
190196
191197 <para >
192198 HQL <literal >UPDATE</literal > statements, by default do not effect the
@@ -198,15 +204,17 @@ session.Close();]]></programlisting>
198204 This is achieved by adding the <literal >VERSIONED</literal > keyword after the <literal >UPDATE</literal >
199205 keyword.
200206 </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)
207215 .ExecuteUpdate();
208- tx.Commit();
209- session.Close(); ]]> </programlisting >
216+ tx.Commit();
217+ } ]]> </programlisting >
210218
211219 <para >
212220 Note that custom version types (<literal >NHibernate.Usertype.IUserVersionType</literal >)
@@ -218,16 +226,16 @@ session.Close();]]></programlisting>
218226 method:
219227 </para >
220228
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)
228236 .ExecuteUpdate();
229- tx.Commit();
230- session.Close(); ]]> </programlisting >
237+ tx.Commit();
238+ } ]]> </programlisting >
231239
232240 <para >
233241 The <literal >int</literal > value returned by the <literal >IQuery.ExecuteUpdate()</literal >
@@ -302,14 +310,14 @@ session.Close();]]></programlisting>
302310 An example HQL <literal >INSERT</literal > statement execution:
303311 </para >
304312
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)
310318 .ExecuteUpdate();
311- tx.Commit();
312- session.Close(); ]]> </programlisting >
319+ tx.Commit();
320+ } ]]> </programlisting >
313321
314322 </sect1 >
315323
0 commit comments