Last active
September 25, 2019 09:55
-
-
Save dzagorovsky/b8064c97ba647ed453ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import play.db.jpa.JPA; | |
public class FirstDao { | |
public int dbCall() { | |
return JPA.em() | |
.createNativeQuery("DELETE FROM table WHERE id=1") | |
.executeUpdate(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import play.db.jpa.JPAApi; | |
import play.libs.F.Promise; | |
import javax.inject.Singleton; | |
import static play.libs.F.Promise.promise; | |
@Singleton | |
public class SomeService { | |
private JPAApi jpaApi; | |
private FirstDao firstDao; | |
private SecondDao secondDao; | |
public SomeService(JPAApi jpaApi, FirstDao firstDao, SecondDao secondDao) { | |
this.jpaApi = jpaApi; | |
this.firstDao = firstDao; | |
this.secondDao = secondDao; | |
} | |
public Promise<Void> doWork() { | |
return promise(() -> jpaApi.withTransaction(() -> { | |
firstDao.dbCall(); | |
secondDao.dbCall(); | |
return null; | |
}), dbExecutionContext); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import play.db.jpa.JPAApi; | |
import play.libs.F.Promise; | |
import javax.inject.Singleton; | |
import static play.libs.F.Promise.promise; | |
@Singleton | |
public class SomeService { | |
private JPAApi jpaApi; | |
private FirstDao firstDao; | |
private SecondDao secondDao; | |
public SomeService(JPAApi jpaApi, FirstDao firstDao, SecondDao secondDao) { | |
this.jpaApi = jpaApi; | |
this.firstDao = firstDao; | |
this.secondDao = secondDao; | |
} | |
public Promise<Void> doWork() { | |
return promise(new F.Function0<Object>() { | |
@Override | |
public Object apply() throws Throwable { | |
return jpaApi.withTransaction(new F.Function0<Object>() { | |
@Override | |
public Object apply() throws Throwable { | |
firstDao.dbCall(); | |
secondDao.dbCall(); | |
return null; | |
} | |
}); | |
} | |
}, dbExecutionContext); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment