onClose callback can be used to release allocated resources
like DB connections. Discussion #1507
Some beans can depend on each other.
An example: classic Web application consists of Controller, Service, Database.
In this case, instantiation order is:
1. Database
2. Service - implements business logic, requires Database
3. Controller - REST controller, requires Service
On application graceful shutdown, onClose invocation order must be reversed:
1. Controller - created last, destroyed first
2. Service - destroyed after Controller to avoid use-after-close
3. Database - destroyed after Service to avoid use-after-close
This commit delivers implementation of onClose calls ordering.
When instance factory creates its first instance, it means that all dependencies are also created. So it's enough to sort factories by 'first instance creation,' and then reverse the sequence when invoking onClose.
Implementation:
global 'instance creation order' atomic counter is created
each instance factory has 'instance creation order' position
when factory creates its first instance
'instance creation order' counter is incremented
the counter's value is saved into 'instance creation order' position of the factory
factories can be sorted by 'instance creation order' position, this can be used to order onClose calls
Fixes #1510
onClose
callback can be used to release allocated resources like DB connections. Discussion #1507Some beans can depend on each other.
An example: classic Web application consists of
Controller
,Service
,Database
.In this case, instantiation order is: 1.
Database
2.Service
- implements business logic, requiresDatabase
3.Controller
- REST controller, requiresService
On application graceful shutdown,
onClose
invocation order must be reversed: 1.Controller
- created last, destroyed first 2.Service
- destroyed afterController
to avoid use-after-close 3.Database
- destroyed afterService
to avoid use-after-closeThis commit delivers implementation of
onClose
calls ordering.When instance factory creates its first instance, it means that all dependencies are also created. So it's enough to sort factories by 'first instance creation,' and then reverse the sequence when invoking
onClose
.Implementation:
onClose
calls