User Tools
development:grails:exceptionresolution
Differences
This shows you the differences between two versions of the page.
| development:grails:exceptionresolution [2018/03/16 10:47] – created ashevyakov | development:grails:exceptionresolution [2021/06/25 10:09] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Exception Resolution Strategies ====== | ||
| + | |||
| + | ===== Exception " | ||
| + | |||
| + | ==== Exception ==== | ||
| + | < | ||
| + | org.hibernate.LazyInitializationException: | ||
| + | </ | ||
| + | |||
| + | ==== Resolution ==== | ||
| + | Use **attach()** to attach the fetched object to the current session. | ||
| + | |||
| + | ==== Example ==== | ||
| + | |||
| + | === Exception Present === | ||
| + | < | ||
| + | def children = ne.children | ||
| + | children.eachWithIndex { ne, index -> log.info ne.name } | ||
| + | </ | ||
| + | |||
| + | === No Exception Present === | ||
| + | < | ||
| + | ne.attach() | ||
| + | def children = ne.children | ||
| + | children.eachWithIndex { ne, index -> log.info ne.name } | ||
| + | </ | ||
| + | |||
| + | \\ | ||
| + | |||
| + | ===== Exceptions " | ||
| + | |||
| + | ==== Exception Examples (Ticketer) ==== | ||
| + | < | ||
| + | org.hibernate.StaleObjectStateException: | ||
| + | </ | ||
| + | < | ||
| + | org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: | ||
| + | </ | ||
| + | < | ||
| + | org.springframework.dao.DuplicateKeyException: | ||
| + | </ | ||
| + | |||
| + | ==== 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/ | ||
| + | |||
| + | ==== 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' | ||
| + | < | ||
| + | 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 " | ||
| + | </ | ||
| + | |||