I will explain my problem in the form of an example. Suppose we want to use both databases in a transaction. Data is edited in database 1 (for example, Postgres) and then added to database 2. Finally, we want to commit the changes (uow.commit()). It is possible that the first database is committed and a problem occurs when the data is committed in the second database.
I used unit of work and outbox pattern but this problem was not solved.
My unit of work has two different databases, neo4j, postgres (I don't know if this is correct or not, or how many databases can one unit of work have?) Both databases are partners in the logic part of the program and none of them are read-only.
I have no insistence to use unit of work or outbox, etc. and I want my transactions to be completed successfully in all my databases and if one of them faces a problem, none of them will be done.
I prefer not to use complicated methods like 2 phase commit.
Is there a pattern to solve this problem or do you have a suggestion or a solution? thanks
this is my unit of work.
class UnitOfWork(AbstractUnitOfWork):
def __init__(self, postgres_session_factory=DEFAULT_SESSION_FACTORY, neo4j_driver=NEO4J_DRIVER):
self.session_factory = postgres_session_factory
self.neo4j_driver = neo4j_driver
def __enter__(self):
self.session = self.session_factory() # type: Session
self.neo4j_session = self.neo4j_driver.session()
self.organization = repository.OrganizationRepository(self.session)
self.organization_chart = repositories.OrganizationChartRepository(self.session)
self.organization_chart_node = repositories.OrganizationChartNodeRepository(self.neo4j_session)
return super().__enter__()
def __exit__(self, *args):
super().__exit__(*args)
self.session.close()
def _commit(self):
self.session.commit()
self.neo4j_session.commit()
def rollback(self):
self.session.expunge_all()
self.session.rollback()