[#5378] Add new TransactionListener SPI that hooks into the TransactionProvider lifecycle

This commit is contained in:
lukaseder 2016-07-01 12:55:37 +02:00
parent 311841c9e2
commit 4e1f458e6c
11 changed files with 631 additions and 32 deletions

View File

@ -262,6 +262,12 @@ public interface Configuration extends Serializable {
*/
TransactionProvider transactionProvider();
/**
* Get the configured <code>TransactionListenerProvider</code>s from this
* configuration.
*/
TransactionListenerProvider[] transactionListenerProviders();
/**
* Get this configuration's underlying record mapper provider.
*/
@ -472,6 +478,18 @@ public interface Configuration extends Serializable {
*/
Configuration set(VisitListenerProvider... newVisitListenerProviders);
/**
* Change this configuration to hold a new transaction listener providers.
* <p>
* This method is not thread-safe and should not be used in globally
* available <code>Configuration</code> objects.
*
* @param newTransactionListenerProviders The new transaction listener
* providers to be contained in the changed configuration.
* @return The changed configuration.
*/
Configuration set(TransactionListenerProvider... newTransactionListenerProviders);
/**
* Change this configuration to hold a new converter provider.
* <p>
@ -612,6 +630,16 @@ public interface Configuration extends Serializable {
*/
Configuration derive(VisitListenerProvider... newVisitListenerProviders);
/**
* Create a derived configuration from this one, with new transaction
* listener providers.
*
* @param newTransactionListenerProviders The new transaction listener
* providers to be contained in the derived configuration.
* @return The derived configuration.
*/
Configuration derive(TransactionListenerProvider... newTransactionListenerProviders);
/**
* Create a derived configuration from this one, with new converter
* provider.

View File

@ -0,0 +1,81 @@
/**
* Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq;
/**
* The <code>TransactionListener</code> SPI is used to intercept the
* {@link TransactionProvider}'s transaction events.
*
* @author Lukas Eder
*/
public interface TransactionListener {
/**
* Called before {@link TransactionProvider#begin(TransactionContext)}.
*/
void beginStart(TransactionContext ctx);
/**
* Called after {@link TransactionProvider#begin(TransactionContext)}.
*/
void beginEnd(TransactionContext ctx);
/**
* Called before {@link TransactionProvider#commit(TransactionContext)}.
*/
void commitStart(TransactionContext ctx);
/**
* Called after {@link TransactionProvider#commit(TransactionContext)}.
*/
void commitEnd(TransactionContext ctx);
/**
* Called before {@link TransactionProvider#rollback(TransactionContext)}.
*/
void rollbackStart(TransactionContext ctx);
/**
* Called after {@link TransactionProvider#rollback(TransactionContext)}.
*/
void rollbackEnd(TransactionContext ctx);
}

View File

@ -0,0 +1,80 @@
/**
* Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq;
import org.jooq.impl.DefaultTransactionListenerProvider;
/**
* A provider for {@link TransactionListener} instances.
* <p>
* In order to facilitate the lifecycle management of
* <code>TransactionListener</code> instances that are provided to a jOOQ
* {@link Configuration}, clients can implement this API. To jOOQ, it is thus
* irrelevant, if transaction listeners are stateful or stateless, local to an
* execution, or global to an application.
*
* @author Lukas Eder
* @see TransactionListener
* @see Configuration
*/
@FunctionalInterface
public interface TransactionListenerProvider {
/**
* Provide an <code>TransactionListener</code> instance.
* <p>
* Implementations are free to choose whether this method returns new
* instances at every call or whether the same instance is returned
* repetitively.
* <p>
* A <code>TransactionListener</code> shall be provided exactly once per
* transaction lifecycle, i.e. per call to
* {@link DSLContext#transaction(TransactionalRunnable)} or similar API.
*
* @return An <code>TransactionListener</code> instance.
* @see TransactionListener
* @see TransactionProvider
* @see DefaultTransactionListenerProvider
*/
TransactionListener provide();
}

View File

@ -64,6 +64,7 @@ import org.jooq.ExecutorProvider;
import org.jooq.RecordListenerProvider;
import org.jooq.RecordMapperProvider;
import org.jooq.SQLDialect;
import org.jooq.TransactionListenerProvider;
import org.jooq.TransactionProvider;
import org.jooq.VisitListenerProvider;
import org.jooq.conf.Settings;
@ -83,25 +84,26 @@ public class DefaultConfiguration implements Configuration {
/**
* Serial version UID
*/
private static final long serialVersionUID = 8193158984283234708L;
private static final long serialVersionUID = 8193158984283234708L;
// Configuration objects
private SQLDialect dialect;
private Settings settings;
private ConcurrentHashMap<Object, Object> data;
private SQLDialect dialect;
private Settings settings;
private ConcurrentHashMap<Object, Object> data;
// Non-serializable Configuration objects
private transient ConnectionProvider connectionProvider;
private transient ExecutorProvider executorProvider;
private transient TransactionProvider transactionProvider;
private transient RecordMapperProvider recordMapperProvider;
private transient RecordListenerProvider[] recordListenerProviders;
private transient ExecuteListenerProvider[] executeListenerProviders;
private transient VisitListenerProvider[] visitListenerProviders;
private transient ConverterProvider converterProvider;
private transient ConnectionProvider connectionProvider;
private transient ExecutorProvider executorProvider;
private transient TransactionProvider transactionProvider;
private transient RecordMapperProvider recordMapperProvider;
private transient RecordListenerProvider[] recordListenerProviders;
private transient ExecuteListenerProvider[] executeListenerProviders;
private transient VisitListenerProvider[] visitListenerProviders;
private transient TransactionListenerProvider[] transactionListenerProviders;
private transient ConverterProvider converterProvider;
// Derived objects
private org.jooq.SchemaMapping mapping;
private org.jooq.SchemaMapping mapping;
// -------------------------------------------------------------------------
// XXX: Constructors
@ -137,6 +139,7 @@ public class DefaultConfiguration implements Configuration {
null,
null,
null,
null,
dialect,
SettingsTools.defaultSettings(),
null
@ -160,6 +163,7 @@ public class DefaultConfiguration implements Configuration {
configuration.recordListenerProviders(),
configuration.executeListenerProviders(),
configuration.visitListenerProviders(),
configuration.transactionListenerProviders(),
configuration.converterProvider(),
configuration.dialect(),
configuration.settings(),
@ -173,9 +177,9 @@ public class DefaultConfiguration implements Configuration {
* through reflection.
*
* @deprecated Use
* {@link #DefaultConfiguration(ConnectionProvider, ExecutorProvider, TransactionProvider, RecordMapperProvider, RecordListenerProvider[], ExecuteListenerProvider[], VisitListenerProvider[], ConverterProvider, SQLDialect, Settings, Map)}
* {@link #DefaultConfiguration(ConnectionProvider, ExecutorProvider, TransactionProvider, RecordMapperProvider, RecordListenerProvider[], ExecuteListenerProvider[], VisitListenerProvider[], TransactionListenerProvider[], ConverterProvider, SQLDialect, Settings, Map)}
* instead. This constructor is maintained to provide jOOQ 3.2,
* 3.3, 3.7 backwards-compatibility if called with reflection
* 3.3, 3.7, 3.8 backwards-compatibility if called with reflection
* from Spring configurations.
*/
@Deprecated
@ -195,6 +199,7 @@ public class DefaultConfiguration implements Configuration {
executeListenerProviders,
null,
null,
null,
dialect,
settings,
data
@ -207,9 +212,9 @@ public class DefaultConfiguration implements Configuration {
* through reflection.
*
* @deprecated Use
* {@link #DefaultConfiguration(ConnectionProvider, ExecutorProvider, TransactionProvider, RecordMapperProvider, RecordListenerProvider[], ExecuteListenerProvider[], VisitListenerProvider[], ConverterProvider, SQLDialect, Settings, Map)}
* {@link #DefaultConfiguration(ConnectionProvider, ExecutorProvider, TransactionProvider, RecordMapperProvider, RecordListenerProvider[], ExecuteListenerProvider[], VisitListenerProvider[], TransactionListenerProvider[], ConverterProvider, SQLDialect, Settings, Map)}
* instead. This constructor is maintained to provide jOOQ 3.2,
* 3.3, 3.7 backwards-compatibility if called with reflection
* 3.3, 3.7, 3.8 backwards-compatibility if called with reflection
* from Spring configurations.
*/
@Deprecated
@ -230,6 +235,7 @@ public class DefaultConfiguration implements Configuration {
executeListenerProviders,
null,
null,
null,
dialect,
settings,
data
@ -242,9 +248,9 @@ public class DefaultConfiguration implements Configuration {
* through reflection.
*
* @deprecated Use
* {@link #DefaultConfiguration(ConnectionProvider, ExecutorProvider, TransactionProvider, RecordMapperProvider, RecordListenerProvider[], ExecuteListenerProvider[], VisitListenerProvider[], ConverterProvider, SQLDialect, Settings, Map)}
* {@link #DefaultConfiguration(ConnectionProvider, ExecutorProvider, TransactionProvider, RecordMapperProvider, RecordListenerProvider[], ExecuteListenerProvider[], VisitListenerProvider[], TransactionListenerProvider[], ConverterProvider, SQLDialect, Settings, Map)}
* instead. This constructor is maintained to provide jOOQ 3.2,
* 3.3, 3.7 backwards-compatibility if called with reflection
* 3.3, 3.7, 3.8 backwards-compatibility if called with reflection
* from Spring configurations.
*/
@Deprecated
@ -267,6 +273,7 @@ public class DefaultConfiguration implements Configuration {
executeListenerProviders,
visitListenerProviders,
null,
null,
dialect,
settings,
data
@ -279,9 +286,9 @@ public class DefaultConfiguration implements Configuration {
* through reflection.
*
* @deprecated Use
* {@link #DefaultConfiguration(ConnectionProvider, ExecutorProvider, TransactionProvider, RecordMapperProvider, RecordListenerProvider[], ExecuteListenerProvider[], VisitListenerProvider[], ConverterProvider, SQLDialect, Settings, Map)}
* {@link #DefaultConfiguration(ConnectionProvider, ExecutorProvider, TransactionProvider, RecordMapperProvider, RecordListenerProvider[], ExecuteListenerProvider[], VisitListenerProvider[], TransactionListenerProvider[], ConverterProvider, SQLDialect, Settings, Map)}
* instead. This constructor is maintained to provide jOOQ 3.2,
* 3.3, 3.7 backwards-compatibility if called with reflection
* 3.3, 3.7, 3.8 backwards-compatibility if called with reflection
* from Spring configurations.
*/
@Deprecated
@ -305,6 +312,7 @@ public class DefaultConfiguration implements Configuration {
executeListenerProviders,
visitListenerProviders,
null,
null,
dialect,
settings,
data
@ -317,9 +325,9 @@ public class DefaultConfiguration implements Configuration {
* through reflection.
*
* @deprecated Use
* {@link #DefaultConfiguration(ConnectionProvider, ExecutorProvider, TransactionProvider, RecordMapperProvider, RecordListenerProvider[], ExecuteListenerProvider[], VisitListenerProvider[], ConverterProvider, SQLDialect, Settings, Map)}
* {@link #DefaultConfiguration(ConnectionProvider, ExecutorProvider, TransactionProvider, RecordMapperProvider, RecordListenerProvider[], ExecuteListenerProvider[], VisitListenerProvider[], TransactionListenerProvider[], ConverterProvider, SQLDialect, Settings, Map)}
* instead. This constructor is maintained to provide jOOQ 3.2,
* 3.3, 3.7 backwards-compatibility if called with reflection
* 3.3, 3.7, 3.8 backwards-compatibility if called with reflection
* from Spring configurations.
*/
@Deprecated
@ -343,6 +351,51 @@ public class DefaultConfiguration implements Configuration {
recordListenerProviders,
executeListenerProviders,
visitListenerProviders,
null,
converterProvider,
dialect,
settings,
data
);
}
/**
* Create the actual configuration object.
* <p>
* This constructor has been made package-private to allow for adding new
* configuration properties in the future, without breaking client code.
* Consider creating a configuration by chaining calls to various
* <code>derive()</code> methods.
*
* @deprecated Use
* {@link #DefaultConfiguration(ConnectionProvider, ExecutorProvider, TransactionProvider, RecordMapperProvider, RecordListenerProvider[], ExecuteListenerProvider[], VisitListenerProvider[], TransactionListenerProvider[], ConverterProvider, SQLDialect, Settings, Map)}
* instead. This constructor is maintained to provide jOOQ 3.2,
* 3.3, 3.7, 3.8 backwards-compatibility if called with reflection
* from Spring configurations.
*/
@Deprecated
DefaultConfiguration(
ConnectionProvider connectionProvider,
ExecutorProvider executorProvider,
TransactionProvider transactionProvider,
RecordMapperProvider recordMapperProvider,
RecordListenerProvider[] recordListenerProviders,
ExecuteListenerProvider[] executeListenerProviders,
VisitListenerProvider[] visitListenerProviders,
ConverterProvider converterProvider,
SQLDialect dialect,
Settings settings,
Map<Object, Object> data)
{
this(
connectionProvider,
executorProvider,
transactionProvider,
recordMapperProvider,
recordListenerProviders,
executeListenerProviders,
visitListenerProviders,
null,
converterProvider,
dialect,
settings,
@ -366,6 +419,7 @@ public class DefaultConfiguration implements Configuration {
RecordListenerProvider[] recordListenerProviders,
ExecuteListenerProvider[] executeListenerProviders,
VisitListenerProvider[] visitListenerProviders,
TransactionListenerProvider[] transactionListenerProviders,
ConverterProvider converterProvider,
SQLDialect dialect,
Settings settings,
@ -378,6 +432,7 @@ public class DefaultConfiguration implements Configuration {
set(recordListenerProviders);
set(executeListenerProviders);
set(visitListenerProviders);
set(transactionListenerProviders);
set(converterProvider);
set(dialect);
set(settings);
@ -428,6 +483,7 @@ public class DefaultConfiguration implements Configuration {
recordListenerProviders,
executeListenerProviders,
visitListenerProviders,
transactionListenerProviders,
converterProvider,
dialect,
settings,
@ -448,6 +504,7 @@ public class DefaultConfiguration implements Configuration {
recordListenerProviders,
executeListenerProviders,
visitListenerProviders,
transactionListenerProviders,
converterProvider,
dialect,
settings,
@ -468,6 +525,7 @@ public class DefaultConfiguration implements Configuration {
recordListenerProviders,
executeListenerProviders,
visitListenerProviders,
transactionListenerProviders,
converterProvider,
dialect,
settings,
@ -488,6 +546,7 @@ public class DefaultConfiguration implements Configuration {
recordListenerProviders,
executeListenerProviders,
visitListenerProviders,
transactionListenerProviders,
converterProvider,
dialect,
settings,
@ -508,6 +567,7 @@ public class DefaultConfiguration implements Configuration {
newRecordListenerProviders,
executeListenerProviders,
visitListenerProviders,
transactionListenerProviders,
converterProvider,
dialect,
settings,
@ -528,6 +588,7 @@ public class DefaultConfiguration implements Configuration {
recordListenerProviders,
newExecuteListenerProviders,
visitListenerProviders,
transactionListenerProviders,
converterProvider,
dialect,
settings,
@ -548,6 +609,28 @@ public class DefaultConfiguration implements Configuration {
recordListenerProviders,
executeListenerProviders,
newVisitListenerProviders,
transactionListenerProviders,
converterProvider,
dialect,
settings,
data
);
}
/**
* {@inheritDoc}
*/
@Override
public final Configuration derive(TransactionListenerProvider... newTransactionListenerProviders) {
return new DefaultConfiguration(
connectionProvider,
executorProvider,
transactionProvider,
recordMapperProvider,
recordListenerProviders,
executeListenerProviders,
visitListenerProviders,
newTransactionListenerProviders,
converterProvider,
dialect,
settings,
@ -568,6 +651,7 @@ public class DefaultConfiguration implements Configuration {
recordListenerProviders,
executeListenerProviders,
visitListenerProviders,
transactionListenerProviders,
newConverterProvider,
dialect,
settings,
@ -588,6 +672,7 @@ public class DefaultConfiguration implements Configuration {
recordListenerProviders,
executeListenerProviders,
visitListenerProviders,
transactionListenerProviders,
converterProvider,
newDialect,
settings,
@ -608,6 +693,7 @@ public class DefaultConfiguration implements Configuration {
recordListenerProviders,
executeListenerProviders,
visitListenerProviders,
transactionListenerProviders,
converterProvider,
dialect,
newSettings,
@ -713,6 +799,18 @@ public class DefaultConfiguration implements Configuration {
return this;
}
/**
* {@inheritDoc}
*/
@Override
public final Configuration set(TransactionListenerProvider... newTransactionListenerProviders) {
this.transactionListenerProviders = newTransactionListenerProviders != null
? newTransactionListenerProviders
: new TransactionListenerProvider[0];
return this;
}
@Override
public final Configuration set(ConverterProvider newConverterProvider) {
this.converterProvider = newConverterProvider != null
@ -811,6 +909,13 @@ public class DefaultConfiguration implements Configuration {
set(newVisitListenerProviders);
}
/**
* @see #set(TransactionListenerProvider[])
*/
public final void setTransactionListenerProvider(TransactionListenerProvider... newTransactionListenerProviders) {
set(newTransactionListenerProviders);
}
/**
* @see #set(SQLDialect)
*/
@ -903,6 +1008,14 @@ public class DefaultConfiguration implements Configuration {
return visitListenerProviders;
}
/**
* {@inheritDoc}
*/
@Override
public final TransactionListenerProvider[] transactionListenerProviders() {
return transactionListenerProviders;
}
/**
* {@inheritDoc}
*/
@ -1003,6 +1116,7 @@ public class DefaultConfiguration implements Configuration {
oos.writeObject(cloneSerializables(executeListenerProviders));
oos.writeObject(cloneSerializables(recordListenerProviders));
oos.writeObject(cloneSerializables(visitListenerProviders));
oos.writeObject(cloneSerializables(transactionListenerProviders));
oos.writeObject(converterProvider instanceof Serializable
? converterProvider
@ -1030,6 +1144,7 @@ public class DefaultConfiguration implements Configuration {
executeListenerProviders = (ExecuteListenerProvider[]) ois.readObject();
recordListenerProviders = (RecordListenerProvider[]) ois.readObject();
visitListenerProviders = (VisitListenerProvider[]) ois.readObject();
transactionListenerProviders = (TransactionListenerProvider[]) ois.readObject();
converterProvider = (ConverterProvider) ois.readObject();
}
}

View File

@ -389,15 +389,33 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
DefaultTransactionContext ctx = new DefaultTransactionContext(configuration().derive());
TransactionProvider provider = ctx.configuration().transactionProvider();
TransactionListeners listeners = new TransactionListeners(ctx.configuration());
try {
provider.begin(ctx);
try {
listeners.beginStart(ctx);
provider.begin(ctx);
}
finally {
listeners.beginEnd(ctx);
}
result = transactional.run(ctx.configuration());
provider.commit(ctx);
try {
listeners.commitStart(ctx);
provider.commit(ctx);
}
finally {
listeners.commitEnd(ctx);
}
}
catch (Exception cause) {
ctx.cause(cause);
listeners.rollbackStart(ctx);
try {
provider.rollback(ctx.cause(cause));
provider.rollback(ctx);
}
// [#3718] Use reflection to support also JDBC 4.0
@ -406,6 +424,7 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
cause.addSuppressed(suppress);
}
listeners.rollbackEnd(ctx);
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;

View File

@ -73,9 +73,8 @@ public class DefaultExecuteListenerProvider implements ExecuteListenerProvider,
public static ExecuteListenerProvider[] providers(ExecuteListener... listeners) {
ExecuteListenerProvider[] result = new ExecuteListenerProvider[listeners.length];
for (int i = 0; i < listeners.length; i++) {
for (int i = 0; i < listeners.length; i++)
result[i] = new DefaultExecuteListenerProvider(listeners[i]);
}
return result;
}

View File

@ -73,9 +73,8 @@ public class DefaultRecordListenerProvider implements RecordListenerProvider, Se
public static RecordListenerProvider[] providers(RecordListener... listeners) {
RecordListenerProvider[] result = new RecordListenerProvider[listeners.length];
for (int i = 0; i < listeners.length; i++) {
for (int i = 0; i < listeners.length; i++)
result[i] = new DefaultRecordListenerProvider(listeners[i]);
}
return result;
}

View File

@ -0,0 +1,74 @@
/**
* Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import org.jooq.TransactionContext;
import org.jooq.TransactionListener;
/**
* A publicly available default implementation of {@link TransactionListener}.
* <p>
* Use this to stay compatible with future API changes (i.e. added methods to
* <code>TransactionListener</code>)
*
* @author Lukas Eder
*/
public class DefaultTransactionListener implements TransactionListener {
@Override
public void beginStart(TransactionContext ctx) {}
@Override
public void beginEnd(TransactionContext ctx) {}
@Override
public void commitStart(TransactionContext ctx) {}
@Override
public void commitEnd(TransactionContext ctx) {}
@Override
public void rollbackStart(TransactionContext ctx) {}
@Override
public void rollbackEnd(TransactionContext ctx) {}
}

View File

@ -0,0 +1,106 @@
/**
* Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import java.io.Serializable;
import org.jooq.TransactionListener;
import org.jooq.TransactionListenerProvider;
/**
* A default implementation for {@link TransactionListenerProvider}.
* <p>
* This implementation just wraps an instance of {@link TransactionListener}, always
* providing the same.
*
* @author Lukas Eder
*/
public class DefaultTransactionListenerProvider implements TransactionListenerProvider, Serializable {
/**
* Generated UID.
*/
private static final long serialVersionUID = -2122007794302549679L;
/**
* The delegate listener.
*/
private final TransactionListener listener;
/**
* Convenience method to construct an array of
* <code>DefaultTransactionListenerProvider</code> from an array of
* <code>TransactionListener</code> instances.
*/
public static TransactionListenerProvider[] providers(TransactionListener... listeners) {
TransactionListenerProvider[] result = new TransactionListenerProvider[listeners.length];
for (int i = 0; i < listeners.length; i++)
result[i] = new DefaultTransactionListenerProvider(listeners[i]);
return result;
}
/**
* Create a new provider instance from an argument listener.
*
* @param listener The argument listener.
*/
public DefaultTransactionListenerProvider(TransactionListener listener) {
this.listener = listener;
}
/**
* {@inheritDoc}
*/
@Override
public final TransactionListener provide() {
return listener;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return listener.toString();
}
}

View File

@ -73,9 +73,8 @@ public class DefaultVisitListenerProvider implements VisitListenerProvider, Seri
public static VisitListenerProvider[] providers(VisitListener... listeners) {
VisitListenerProvider[] result = new VisitListenerProvider[listeners.length];
for (int i = 0; i < listeners.length; i++) {
for (int i = 0; i < listeners.length; i++)
result[i] = new DefaultVisitListenerProvider(listeners[i]);
}
return result;
}

View File

@ -0,0 +1,99 @@
/**
* Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import org.jooq.Configuration;
import org.jooq.TransactionContext;
import org.jooq.TransactionListener;
import org.jooq.TransactionListenerProvider;
/**
* @author Lukas Eder
*/
class TransactionListeners implements TransactionListener {
private final TransactionListener[] listeners;
TransactionListeners(Configuration configuration) {
TransactionListenerProvider[] providers = configuration.transactionListenerProviders();
listeners = new TransactionListener[providers.length];
for (int i = 0; i < providers.length; i++)
listeners[i] = providers[i].provide();
}
@Override
public final void beginStart(TransactionContext ctx) {
for (TransactionListener listener : listeners)
listener.beginStart(ctx);
}
@Override
public final void beginEnd(TransactionContext ctx) {
for (TransactionListener listener : listeners)
listener.beginEnd(ctx);
}
@Override
public final void commitStart(TransactionContext ctx) {
for (TransactionListener listener : listeners)
listener.commitStart(ctx);
}
@Override
public final void commitEnd(TransactionContext ctx) {
for (TransactionListener listener : listeners)
listener.commitEnd(ctx);
}
@Override
public final void rollbackStart(TransactionContext ctx) {
for (TransactionListener listener : listeners)
listener.rollbackStart(ctx);
}
@Override
public final void rollbackEnd(TransactionContext ctx) {
for (TransactionListener listener : listeners)
listener.rollbackEnd(ctx);
}
}