[hibernate-dev] inSession / inTransaction

Steve Ebersole steve at hibernate.org
Thu Dec 20 17:16:09 EST 2018


On Thu, Dec 20, 2018 at 1:33 PM Gunnar Morling <gunnar at hibernate.org> wrote:

> Hey,
>
> When discussing this before, there were some doubts about its actual
> usefulness in non-testing, real-world code. E.g. you'd typically
> interact with multiple DAOs/repositories etc. and would have to
> somehow pass the session to each of them.
>

I've written many non-trivial apps in my past that did not use
"DAO/repositories" etc.  Not sure why we'd choose to not implement
something that is useful just because not everyone would use it.  To me, if
something is repeatedly useful in writing tests... it tends to be
more-or-less generally useful.


One other thought is that inTransaction() should allow to return a result
> value.
>

An over-loaded form perhaps, yes I can see that - but non-returning is
valid as well.  So maybe:

public interface SessionFactory ... {
    ...
    void inSession(Consumer<Session> action);
    void inTransaction(Consumer<Session> action);
    void inTransaction(Session session, Consumer<Session> action);

    <T> T inSession(Function<Session,T> action);
    <T> T inTransaction(Function<Session,T> action);
    <T> T inTransaction(Session session, Function<Session,T> action);
}

and

public interface Session ... {
    void inTransaction(Consumer<Session> action);

    <T> T inTransaction(Function<Session,T> action);
}


More information about the hibernate-dev mailing list