Play2 How to manage the transaction from the service layer instead of action layer?

前端 未结 2 909
北海茫月
北海茫月 2021-01-16 17:09

I am using Play2.1.1 Java with JPA2.0 with hibernate implementation.

to control the transaction by code instead of using @transactional like below is the normal JPA

2条回答
  •  太阳男子
    2021-01-16 17:35

    something like this:

    public static User getUserByIdentity(final AuthUserIdentity identity) {
        try {
            return JPA.withTransaction(new play.libs.F.Function0() {
                public User apply() {       
                    return User.findByAuthUserIdentity(identity);
                }
            });
        } catch (Throwable t) {
            throw new RuntimeException(t);
        }       
    }
    

提交回复
热议问题