Skip to content

Order of onClose calls #1510

Closed
Closed
@smichovan

Description

@smichovan

Discussed in #1507

Originally posted by smichovan January 20, 2023
Is there a way how to change order in witch the onClose methods of components are called?

Lets have two services where the latter is dependent of the former:

val appModule = module {
single { Database() } onClose {it?.closeConnectionPool()}
single { ServiceUsingDb(get()) } onClose {it?.useDatabaseForCleanup()}
}

Database component maintains a connection to some DB and the second component accesses the DB through the first component. The "ServiceUsingDb" needs to have DB available in onClose method. As the "ServiceUsingDb" is actually dependent on "Database" I would expect Koin to ensure the right closing order.

I made some research in the Koin code and if I understand it well, the components (instances, mappings) are stored in HashMap, means no specific order is guaranteed. In my case, it may (and does) happen that when ServiceUsingDb onClose is called, the DB is already closed and the cleanup fails.

Have I missed anything? Is there any way how to influence the onClose methods order?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions