Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ name=StorageUtils
authors=AlphaConqueror
description=Utilities for data storage.
version_major=1
version_minor=6
version_minor=7
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,18 @@ public <R> R performTask(final HibernateStorage.@NonNull ResultTask<T, R> task)
final R result = task.execute(taskContext);

transaction.commit();
// marks context closed soo no context actions are done in queue
taskContext.markClosed();
// execute tasks after transaction was successfully committed
taskContext.executeTasks();

return result;
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();

taskContext.markClosed();

Comment on lines +99 to +101
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary new lines.

taskContext.executeRollbackTasks();
}

Expand Down Expand Up @@ -123,6 +128,8 @@ public <R> R performTask(final HibernateStorage.@NonNull ResultTask<T, R> task)
}

throw new CompletionException(e);
} finally {
taskContext.markClosed();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be careful with finally-blocks, since they are always executed, meaning #markClosed is executed twice in some cases.

}
}
} catch (final JDBCConnectionException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ public class StandardTaskContext implements TaskContext {
@NonNull
private final Queue<Runnable> rollbackQueue = new LinkedList<>();

private final Thread owner = Thread.currentThread();
private volatile boolean closed = false;

public StandardTaskContext(@NonNull final Session session) {
this.session = session;
}

@Override
public @NonNull Session session() {
this.checkUse();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep in mind that this will not always work and is easily bypassed. Example:

Session session = context.session();

context.queue(() -> {
  // do something involving the session
});

This ^ is how I usually structure my code.

return this.session;
}

Expand All @@ -52,4 +56,20 @@ public void executeRollbackTasks() {
this.rollbackQueue.poll().run();
}
}

@Override
public void markClosed() {
this.closed = true;
}

@Override
public void checkUse() {
if (closed) {
throw new IllegalStateException("TaskContext used after task finished");
}

if (Thread.currentThread() != owner) {
throw new IllegalStateException("TaskContext used from non-owner thread");
}
}
Comment on lines +65 to +74
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you want exceptions to be thrown in the default case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this method will be overriden in dirtcore

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ public interface TaskContext {
* Executes the runnable tasks upon rollback.
*/
void executeRollbackTasks();

/**
* Marks context as closed.
*/
void markClosed();

/**
* Check if context is used after close.
*/
void checkUse();
}