Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature : Quarkus integration #320

Merged
merged 6 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ jobs:
sed -i "s_\(<version>\)[^<]*_\1${revision}_g" transactionoutbox-guice/README.md
sed -i "s_\(<version>\)[^<]*_\1${revision}_g" transactionoutbox-jooq/README.md
sed -i "s_\(<version>\)[^<]*_\1${revision}_g" transactionoutbox-spring/README.md
sed -i "s_\(<version>\)[^<]*_\1${revision}_g" transactionoutbox-cdi/README.md
RomainWilbert marked this conversation as resolved.
Show resolved Hide resolved
sed -i "s_\(implementation 'com.gruelbox:transactionoutbox-core:\)[^']*_\1${revision}_g" README.md
sed -i "s_\(implementation 'com.gruelbox:transactionoutbox-guice:\)[^']*_\1${revision}_g" transactionoutbox-guice/README.md
sed -i "s_\(implementation 'com.gruelbox:transactionoutbox-jooq:\)[^']*_\1${revision}_g" transactionoutbox-jooq/README.md
sed -i "s_\(implementation 'com.gruelbox:transactionoutbox-spring:\)[^']*_\1${revision}_g" transactionoutbox-spring/README.md
sed -i "s_\(implementation 'com.gruelbox:transactionoutbox-cdi:\)[^']*_\1${revision}_g" transactionoutbox-cdi/README.md
env:
GITHUB_TOKEN: ${{ github.token }}
SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }}
Expand All @@ -55,10 +57,12 @@ jobs:
sed -i "s_\(<version>\)[^<]*_\1${revision}_g" transactionoutbox-guice/README.md
sed -i "s_\(<version>\)[^<]*_\1${revision}_g" transactionoutbox-jooq/README.md
sed -i "s_\(<version>\)[^<]*_\1${revision}_g" transactionoutbox-spring/README.md
sed -i "s_\(<version>\)[^<]*_\1${revision}_g" transactionoutbox-cdi/README.md
sed -i "s_\(implementation 'com.gruelbox:transactionoutbox-core:\)[^']*_\1${revision}_g" README.md
sed -i "s_\(implementation 'com.gruelbox:transactionoutbox-guice:\)[^']*_\1${revision}_g" transactionoutbox-guice/README.md
sed -i "s_\(implementation 'com.gruelbox:transactionoutbox-jooq:\)[^']*_\1${revision}_g" transactionoutbox-jooq/README.md
sed -i "s_\(implementation 'com.gruelbox:transactionoutbox-spring:\)[^']*_\1${revision}_g" transactionoutbox-spring/README.md
sed -i "s_\(implementation 'com.gruelbox:transactionoutbox-cdi:\)[^']*_\1${revision}_g" transactionoutbox-cdi/README.md

- name: Create version update pull request
uses: gruelbox/create-pull-request@v2
Expand Down
6 changes: 3 additions & 3 deletions transactionoutbox-cdi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ Create your `TransactionOutbox` as a bean:
@Produces
public TransactionManager cdiTransactionManager(DataSource datasource, TransactionSynchronizationRegistry tsr) throws SQLException
{
return new CdiTransactionManager(datasource, tsr);
return CdiTransactionManager.create(datasource, tsr);
RomainWilbert marked this conversation as resolved.
Show resolved Hide resolved
}

@Produces
public TransactionOutbox transactionOutbox(TransactionManager transactionManager, CdiInstanciator instanciator)
public TransactionOutbox transactionOutbox(TransactionManager transactionManager)
{
return TransactionOutbox.builder().instantiator(instanciator).blockAfterAttempts(1).transactionManager(transactionManager).persistor(Persistor.forDialect(Dialect.H2)).build();
return TransactionOutbox.builder().instantiator(CdiInstantiator.create()).transactionManager(transactionManager).persistor(Persistor.forDialect(Dialect.H2)).build();
}
```

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.gruelbox.transactionoutbox;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.spi.CDI;

@ApplicationScoped
public class CdiInstantiator extends AbstractFullyQualifiedNameInstantiator {

public static CdiInstantiator create() {
return new CdiInstantiator();
}

private CdiInstantiator() {}

@Override
protected Object createInstance(Class<?> clazz) {
return CDI.current().select(clazz).get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,173 +2,150 @@

import static com.gruelbox.transactionoutbox.Utils.uncheck;

import io.agroal.api.AgroalDataSource;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.sql.DataSource;
import javax.transaction.Status;
import javax.transaction.Synchronization;
import javax.transaction.TransactionSynchronizationRegistry;
import javax.transaction.Transactional;
import javax.transaction.Transactional.TxType;


public class CdiTransactionManager implements ThreadLocalContextTransactionManager
{
private final CdiTransaction transactionInstance;

private TransactionSynchronizationRegistry tsr;

public CdiTransactionManager(DataSource datasource, TransactionSynchronizationRegistry tsr) throws SQLException
{
transactionInstance = new CdiTransaction(datasource);
this.tsr = tsr;
}

@Override
@Transactional(value = TxType.REQUIRES_NEW)
public void inTransaction(Runnable runnable)
{
uncheck(() -> inTransactionReturnsThrows(ThrowingTransactionalSupplier.fromRunnable(runnable)));
}

@Override
@Transactional(value = TxType.REQUIRES_NEW)
public void inTransaction(TransactionalWork work)
{
uncheck(() -> inTransactionReturnsThrows(ThrowingTransactionalSupplier.fromWork(work)));
}

@Override
@Transactional(value = TxType.REQUIRES_NEW)
public <T, E extends Exception> T inTransactionReturnsThrows(ThrowingTransactionalSupplier<T, E> work) throws E
{
return work.doWork(transactionInstance);
}

@Override
public <T, E extends Exception> T requireTransactionReturns(ThrowingTransactionalSupplier<T, E> work) throws E, NoTransactionActiveException
{
if (tsr.getTransactionStatus() != Status.STATUS_ACTIVE)
{
throw new NoTransactionActiveException();
}

return work.doWork(transactionInstance);
}

private final class CdiTransaction implements Transaction
{

private DataSource datasource;

public CdiTransaction(DataSource datasource) throws SQLException
{
this.datasource = datasource;
}

@Override
public final Connection connection()
{
try
{
return datasource.getConnection();
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
/** Transaction manager which uses cdi and jta. */
@ApplicationScoped
public class CdiTransactionManager implements ThreadLocalContextTransactionManager {

private final CdiTransaction transactionInstance = new CdiTransaction();

private final DataSource datasource;

private final TransactionSynchronizationRegistry tsr;

@Inject
public CdiTransactionManager(AgroalDataSource datasource, TransactionSynchronizationRegistry tsr)
throws SQLException {
this.datasource = datasource;
this.tsr = tsr;
}

@Override
@Transactional(value = TxType.REQUIRES_NEW)
public void inTransaction(Runnable runnable) {
uncheck(() -> inTransactionReturnsThrows(ThrowingTransactionalSupplier.fromRunnable(runnable)));
}

@Override
@Transactional(value = TxType.REQUIRES_NEW)
public void inTransaction(TransactionalWork work) {
uncheck(() -> inTransactionReturnsThrows(ThrowingTransactionalSupplier.fromWork(work)));
}

@Override
@Transactional(value = TxType.REQUIRES_NEW)
public <T, E extends Exception> T inTransactionReturnsThrows(
ThrowingTransactionalSupplier<T, E> work) throws E {
return work.doWork(transactionInstance);
}

@Override
public <T, E extends Exception> T requireTransactionReturns(
ThrowingTransactionalSupplier<T, E> work) throws E, NoTransactionActiveException {
if (tsr.getTransactionStatus() != Status.STATUS_ACTIVE) {
throw new NoTransactionActiveException();
}

return work.doWork(transactionInstance);
}

private final class CdiTransaction implements Transaction {

public final Connection connection() {
try {
return datasource.getConnection();
RomainWilbert marked this conversation as resolved.
Show resolved Hide resolved
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

@Override
public PreparedStatement prepareBatchStatement(String sql)
{
BatchCountingStatement preparedStatement = Utils.uncheckedly(() -> BatchCountingStatementHandler.countBatches(connection().prepareStatement(sql)));
@Override
public PreparedStatement prepareBatchStatement(String sql) {
BatchCountingStatement preparedStatement =
Utils.uncheckedly(
() -> BatchCountingStatementHandler.countBatches(connection().prepareStatement(sql)));

tsr.registerInterposedSynchronization(new Synchronization()
{
tsr.registerInterposedSynchronization(
new Synchronization() {
@Override
public void beforeCompletion()
{
if (preparedStatement.getBatchCount() != 0)
{
Utils.uncheck(preparedStatement::executeBatch);
}
public void beforeCompletion() {
if (preparedStatement.getBatchCount() != 0) {
Utils.uncheck(preparedStatement::executeBatch);
}
}

@Override
public void afterCompletion(int status)
{
Utils.safelyClose(preparedStatement);
public void afterCompletion(int status) {
Utils.safelyClose(preparedStatement);
}
});
});

return preparedStatement;
}
return preparedStatement;
}

@Override
public void addPostCommitHook(Runnable runnable)
{
tsr.registerInterposedSynchronization(new Synchronization()
{
@Override
public void addPostCommitHook(Runnable runnable) {
tsr.registerInterposedSynchronization(
new Synchronization() {
@Override
public void beforeCompletion()
{
}
public void beforeCompletion() {}

@Override
public void afterCompletion(int status)
{
runnable.run();
public void afterCompletion(int status) {
runnable.run();
}
});
}
}
});
}
}

private interface BatchCountingStatement extends PreparedStatement
{
int getBatchCount();
}
private interface BatchCountingStatement extends PreparedStatement {
int getBatchCount();
}

private static final class BatchCountingStatementHandler implements InvocationHandler
{
private static final class BatchCountingStatementHandler implements InvocationHandler {

private final PreparedStatement delegate;
private final PreparedStatement delegate;

private int count = 0;
private int count = 0;

private BatchCountingStatementHandler(PreparedStatement delegate)
{
this.delegate = delegate;
}
private BatchCountingStatementHandler(PreparedStatement delegate) {
this.delegate = delegate;
}

static BatchCountingStatement countBatches(PreparedStatement delegate)
{
return (BatchCountingStatement) Proxy.newProxyInstance(BatchCountingStatementHandler.class.getClassLoader(), new Class[] { BatchCountingStatement.class },
new BatchCountingStatementHandler(delegate));
}
static BatchCountingStatement countBatches(PreparedStatement delegate) {
return (BatchCountingStatement)
Proxy.newProxyInstance(
BatchCountingStatementHandler.class.getClassLoader(),
new Class[] {BatchCountingStatement.class},
new BatchCountingStatementHandler(delegate));
}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
if ("getBatchCount".equals(method.getName()))
{
return count;
}
try
{
return method.invoke(delegate, args);
}
finally
{
if ("addBatch".equals(method.getName()))
{
++count;
}
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if ("getBatchCount".equals(method.getName())) {
return count;
}
try {
return method.invoke(delegate, args);
} finally {
if ("addBatch".equals(method.getName())) {
++count;
}
}
}
}
}
}
Loading