Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Commit

Permalink
Rename execute methods to sync/async
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmihalcea committed Feb 23, 2015
1 parent 8400fd2 commit 4d16a32
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 21 deletions.
Expand Up @@ -74,7 +74,7 @@ public Void execute(final Session session) {
final P post = (P)
session.get(postClass, 1L);
try {
executeAndWait(new Callable<Void>() {
executeSync(new Callable<Void>() {
@Override
public Void call() throws Exception {
return doInTransaction(new TransactionCallable<Void>() {
Expand Down
Expand Up @@ -8,8 +8,6 @@
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -46,7 +44,7 @@ public Void execute(Session session) {
public Void execute(Session session) {
final Product product = (Product) session.get(Product.class, 1L);
try {
executeAndWait(new Callable<Void>() {
executeSync(new Callable<Void>() {
@Override
public Void call() throws Exception {
return doInTransaction(new TransactionCallable<Void>() {
Expand Down
Expand Up @@ -73,7 +73,7 @@ public Void execute(Session session) {
Repository repository = (Repository) session.get(Repository.class, 1L);
session.buildLockRequest(new LockOptions(LockMode.OPTIMISTIC_FORCE_INCREMENT)).lock(repository);

executeAndWait(new Callable<Void>() {
executeSync(new Callable<Void>() {
@Override
public Void call() throws Exception {
return doInTransaction(new TransactionCallable<Void>() {
Expand Down
Expand Up @@ -30,14 +30,14 @@ public void beforeTransactionCompletion(Transaction tx) {
if(ready.get()) {
LOGGER.info("Overwrite product price asynchronously");

executeNoWait(new Callable<Void>() {
executeAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
Session _session = getSessionFactory().openSession();
_session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
try(PreparedStatement ps = connection.prepareStatement("UPDATE product set price = 14.49 WHERE id = 1")) {
try (PreparedStatement ps = connection.prepareStatement("UPDATE product set price = 14.49 WHERE id = 1")) {
ps.executeUpdate();
}
}
Expand Down
@@ -1,14 +1,11 @@
package com.vladmihalcea.hibernate.masterclass.laboratory.concurrency;

import com.vladmihalcea.hibernate.masterclass.laboratory.util.AbstractTest;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.Session;
import org.hibernate.dialect.lock.OptimisticEntityLockException;
import org.junit.Before;
import org.junit.Test;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.concurrent.Callable;

Expand All @@ -30,7 +27,7 @@ public void testImplicitOptimisticLocking() {
public Void execute(Session session) {
final Product product = (Product) session.get(Product.class, 1L);
try {
executeAndWait(new Callable<Void>() {
executeSync(new Callable<Void>() {
@Override
public Void call() throws Exception {
return doInTransaction(new TransactionCallable<Void>() {
Expand Down Expand Up @@ -63,7 +60,7 @@ public void testExplicitOptimisticLocking() {
public Void execute(Session session) {
final Product product = (Product) session.get(Product.class, 1L, new LockOptions(LockMode.OPTIMISTIC));

executeAndWait(new Callable<Void>() {
executeSync(new Callable<Void>() {
@Override
public Void call() throws Exception {
return doInTransaction(new TransactionCallable<Void>() {
Expand Down
Expand Up @@ -77,7 +77,7 @@ public Void execute(Session session) {
Repository repository = (Repository) session.get(Repository.class, 1L);
session.buildLockRequest(new LockOptions(LockMode.PESSIMISTIC_FORCE_INCREMENT)).lock(repository);

executeNoWait(new Callable<Void>() {
executeAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
return doInTransaction(new TransactionCallable<Void>() {
Expand Down Expand Up @@ -123,7 +123,7 @@ public Void execute(Session session) {
try {
Repository repository = (Repository) session.get(Repository.class, 1L);

executeAndWait(new Callable<Void>() {
executeSync(new Callable<Void>() {
@Override
public Void call() throws Exception {
return doInTransaction(new TransactionCallable<Void>() {
Expand Down
Expand Up @@ -6,7 +6,6 @@
import org.hibernate.Session;
import org.hibernate.StaleObjectStateException;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import javax.persistence.Entity;
Expand Down Expand Up @@ -58,7 +57,7 @@ private void testPessimisticLocking(ProductLockRequestCallable aliceLockRequestC
Product product = (Product) session.get(Product.class, 1L);
aliceLockRequestCallable.lock(session, product);

executeNoWait(
executeAsync(
() -> {
doInTransaction(_session -> {
Product _product = (Product) _session.get(Product.class, 1L);
Expand Down
Expand Up @@ -160,11 +160,11 @@ protected <T> T doInTransaction(TransactionCallable<T> callable) {
return result;
}

protected <T> void executeAndWait(Callable<T> callable) {
executeAndWait(Collections.singleton(callable));
protected <T> void executeSync(Callable<T> callable) {
executeSync(Collections.singleton(callable));
}

protected <T> void executeAndWait(Collection<Callable<T>> callables) {
protected <T> void executeSync(Collection<Callable<T>> callables) {
try {
List<Future<T>> futures = executorService.invokeAll(callables);
for (Future<T> future : futures) {
Expand All @@ -175,7 +175,7 @@ protected <T> void executeAndWait(Collection<Callable<T>> callables) {
}
}

protected <T> void executeNoWait(Callable<T> callable, final Callable<Void> completionCallback) {
protected <T> void executeAsync(Callable<T> callable, final Callable<Void> completionCallback) {
final Future<T> future = executorService.submit(callable);
new Thread(() -> {
while (!future.isDone()) {
Expand All @@ -193,7 +193,7 @@ protected <T> void executeNoWait(Callable<T> callable, final Callable<Void> comp
}).start();
}

protected <T> Future<T> executeNoWait(Callable<T> callable) {
protected <T> Future<T> executeAsync(Callable<T> callable) {
return executorService.submit(callable);
}

Expand Down

0 comments on commit 4d16a32

Please sign in to comment.