title: LazyInitializationException for multiple transactions per request
body: <p>I have a grails service which performs an operation on a given domain object. This operation may fail, and if it does, I want the transaction for that operation to roll back.</p>
  
  <p>I also have a controller that loops over all instances of this domain class, calling the service on each one. If the service operation fails for a given object, I want to continue processing the rest of the domain objects, each in its own independent transaction.</p>
  
  <p>Just to clarify, <strong>I need all rooms within a house (see domain classes below) to be processed within a single transaction, but a separate transaction per house.</strong></p>
  
  <p>Is there a way to achieve this? Below is what I tried, but I keep getting the exception below when I try to process subsequent objects after an operation on one of them has failed. Could it be maybe that the Hibernate session is getting closed when the transaction is rolled back?</p>
  
  <p>(Note: Switching to eager loading is probably not an option for me, as our real domain classes actually contain multiple hasMany relationships.)</p>
  
  <p>Exception:</p>
  
  <pre><code>org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.blah.House.rooms, no session or session was closed
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>class DataService {
      static transactional = true
  
      def processStuff(House house){
          house.rooms.each{
              if(someCondition){
                  throw new RuntimeException('failed') // will cause txn to roll back
              }
          }
      }
  }
  
  class DataController {
  
      def dataService
  
      def doSomething(){
          def houses = House.findAll()
          houses.each{ house -&gt;
              dataService.processStuff(house)
          }
          render(view:'someView')
      }
  }
  
  class House {
      def hasMany = [rooms: Room]
  }
  class Room {
  }
  </code></pre>