User Tools

Site Tools


development:grails:exceptionresolution

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

development:grails:exceptionresolution [2018/03/16 10:47] – created ashevyakovdevelopment:grails:exceptionresolution [2021/06/25 10:09] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +====== Exception Resolution Strategies ======
 +
 +===== Exception "LazyInitializationException" =====
 +
 +==== Exception ====
 +<code>
 +org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: *class name here*, no session or session was closed
 +</code>
 +
 +==== Resolution ====
 +Use **attach()** to attach the fetched object to the current session.
 +
 +==== Example ====
 +
 +=== Exception Present ===
 +<code>
 +def children = ne.children
 +children.eachWithIndex { ne, index -> log.info ne.name }
 +</code>
 +
 +=== No Exception Present ===
 +<code>
 +ne.attach()
 +def children = ne.children
 +children.eachWithIndex { ne, index -> log.info ne.name }
 +</code>
 +
 +\\
 +
 +===== Exceptions "StaleObjectStateException", "HibernateOptimisticLockingFailureException", "DuplicateKeyException" =====
 +
 +==== Exception Examples (Ticketer) ====
 +<code>
 +org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [*domainName*#*domain ID*]
 +</code>
 +<code>
 +org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Object of class [com.errigal.ticketer.Ticket] with identifier [510111]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.errigal.ticketer.Ticket#510111]
 +</code>
 +<code>
 +org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was already associated with the session: [com.errigal.ticketer.TicketStatus#591611]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.errigal.ticketer.TicketStatus#591611]
 +</code>
 +
 +==== Resolution 1 ====
 +Use **attach()** to attach the fetched object to the current session.
 +
 +==== Resolution 2 ====
 +Use **merge()** to attach the changes made to the object by different session/transaction to the current session object.
 +
 +==== Resolution 3 ====
 +Fetch the required object ID before starting a new session and then create an new object inside the newly started session using its id, update the required values, and then save.
 +
 +==== Resolution 3 Example (Ticketer, 'Alarm Clear Received' State) ====
 +<code>
 +arg , defaultArg , ticket ->
 +
 +Long ticketId = ticket.id
 +com.errigal.ticketer.Ticket.withNewSession { sess ->
 +    def newTicket = com.errigal.ticketer.Ticket.get(ticketId)
 +    newTicket.dueDate = new Date()
 +    newTicket.priority = 1 as Integer
 +    newTicket.save()
 +    log.info "Set Due Date of Ticket ${newTicket.id} to be ${newTicket.dueDate} and Priority to ${newTicket.priority}."
 +    sess.flush()
 +}
 +
 +log.info "Check: Ticket ${ticket.id} Due Date ${ticket.dueDate} and Priority ${ticket.priority}."
 +</code>
 +