Hello guys,

many of us possibly work on SaaS server applications,
where we often have the following layers:

- Manager/Services Layer (Opens up transactions)
- DAO Layer (Interacts with the database)

i now would like to make use of jooqs nested transactions, as they are 
described in: https://blog.jooq.org/nested-transactions-in-jooq/ but i also 
would like to pass this "jooq-context" of each transaction (with additional 
data typical for SaaS like tenantId, languageId, ...) into the dao-layer, 
so the dao-instances will only work on this transaction and have the 
additional data (tenantId,...) available for filtering etc.

i have already written a class that implements the "Try-With-Resources" 
pattern, which works correctly. See: 
https://github.com/funkrusher/fk-framework-quarkus-jooq/blob/main/fk_core/src/main/java/org/fk/core/jooq/JooqContext.java

i can use it as follows in the manager layer of my app:

```java
public void deleteAndInsert(List<AuthorRecord> insertAuthors) {

  // transaction1
  try (JooqContext t1JooqContext = 
jooqContextFactory.createJooqContext(requestContext)) {

    AuthorDao t1AuthorDao = daoFactory.createAuthorDao(t1JooqContext);
    List<AuthorRecord> existingAuthors = 
t1AuthorDao.selectSomeAuthors(insertAuthors);

    // transaction2
    try (JooqContext t2JooqContext = 
jooqContextFactory.createJooqContext(requestContext)) {
      AuthorDao t2AuthorDao = daoFactory.createAuthorDao(t2JooqContext);
      t2AuthorDao.deleteByAuthorIds(existingAuthors);
      t2JooqContext.commit();
    }

    // transaction3
    try {
      try (JooqContext t3JooqContext = 
jooqContextFactory.createJooqContext(requestContext)) {
        AuthorDao t3AuthorDao = 
daoFactory.createProductRecordDAO(t3JooqContext);
        t3AuthorDao.insert(insertAuthors);
        t3JooqContext.commit();
      }
    } catch (Exception e) {
      LOGGER.info(e);
    }

  } catch (Exception e) {
    e.printStackTrace();
  }
}
```

within the JooqContext class i work with jooqs:
- dsl.startTransaction (at start of try-with-resources)
- dsl.commit (must be called explicit from outside)
- dsl.rollback when the try-block has not been completed with a "commit" 
call.

the only problem i currently have is, that i explicitly need to "commit" 
every transaction at the end of my Try-With-Resources block.

I now want to ask you guys:
- is there a better way to achieve what i need (Manager-Layer and Dao-Layer 
separation with shared request-data like tenantId + transaction management 
in Manager-Layer)

and if its ok what i do, 
is there a way to make the try-with-resources call commit automatically at 
the end of the Try-Block.

best regards,

Bernd

-- 
You received this message because you are subscribed to the Google Groups "jOOQ 
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jooq-user/ac7c71c9-a0c0-452f-816a-04e66f47b59fn%40googlegroups.com.

Reply via email to