Skip to content

Commit

Permalink
Merge pull request #156 from christophercurrie/lambda-friendly-callbacks
Browse files Browse the repository at this point in the history
Add lambda-friendly callback interfaces
  • Loading branch information
stevenschlansker committed Jul 14, 2015
2 parents 52d2c73 + 437349f commit 70b4146
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/main/java/org/skife/jdbi/v2/BasicHandle.java
Expand Up @@ -327,6 +327,17 @@ public <ReturnType> ReturnType inTransaction(TransactionCallback<ReturnType> cal
return transactions.inTransaction(this, callback);
}

@Override
public void inTransaction(final TransactionConsumer callback)
{
transactions.inTransaction(this, new VoidTransactionCallback() {
@Override
protected void execute(Handle handle, TransactionStatus status) throws Exception {
callback.inTransaction(handle, status);
}
});
}

@Override
public <ReturnType> ReturnType inTransaction(TransactionIsolationLevel level,
TransactionCallback<ReturnType> callback)
Expand Down Expand Up @@ -355,6 +366,17 @@ public <ReturnType> ReturnType inTransaction(TransactionIsolationLevel level,
}
}

@Override
public void inTransaction(TransactionIsolationLevel level, final TransactionConsumer callback)
{
inTransaction(level, new VoidTransactionCallback() {
@Override
protected void execute(Handle handle, TransactionStatus status) throws Exception {
callback.inTransaction(handle, status);
}
});
}

@Override
public List<Map<String, Object>> select(String sql, Object... args)
{
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/org/skife/jdbi/v2/DBI.java
Expand Up @@ -23,13 +23,15 @@
import org.skife.jdbi.v2.tweak.ConnectionFactory;
import org.skife.jdbi.v2.tweak.ContainerFactory;
import org.skife.jdbi.v2.tweak.HandleCallback;
import org.skife.jdbi.v2.tweak.HandleConsumer;
import org.skife.jdbi.v2.tweak.ResultSetMapper;
import org.skife.jdbi.v2.tweak.SQLLog;
import org.skife.jdbi.v2.tweak.StatementBuilder;
import org.skife.jdbi.v2.tweak.StatementBuilderFactory;
import org.skife.jdbi.v2.tweak.StatementLocator;
import org.skife.jdbi.v2.tweak.StatementRewriter;
import org.skife.jdbi.v2.tweak.TransactionHandler;
import org.skife.jdbi.v2.tweak.VoidHandleCallback;
import org.skife.jdbi.v2.tweak.transactions.LocalTransactionHandler;

import javax.sql.DataSource;
Expand Down Expand Up @@ -286,6 +288,28 @@ public <ReturnType> ReturnType withHandle(HandleCallback<ReturnType> callback) t
}
}

/**
* A convenience function which manages the lifecycle of a handle and yields it to a callback
* for use by clients.
*
* @param callback A callback which will receive an open Handle
*
* @return the value returned by callback
*
* @throws CallbackFailedException Will be thrown if callback raises an exception. This exception will
* wrap the exception thrown by the callback.
*/
@Override
public void withHandle(final HandleConsumer callback) throws CallbackFailedException
{
withHandle(new VoidHandleCallback() {
@Override
protected void execute(Handle handle) throws Exception {
callback.withHandle(handle);
}
});
}

/**
* A convenience function which manages the lifecycle of a handle and yields it to a callback
* for use by clients. The handle will be in a transaction when the callback is invoked, and
Expand All @@ -311,6 +335,18 @@ public ReturnType withHandle(Handle handle) throws Exception
});
}

@Override
public void inTransaction(final TransactionConsumer callback) throws CallbackFailedException
{
withHandle(new HandleConsumer() {
@Override
public void withHandle(Handle handle) throws Exception
{
handle.inTransaction(callback);
}
});
}

@Override
public <ReturnType> ReturnType inTransaction(final TransactionIsolationLevel isolation, final TransactionCallback<ReturnType> callback) throws CallbackFailedException
{
Expand All @@ -323,6 +359,18 @@ public ReturnType withHandle(Handle handle) throws Exception
});
}

@Override
public void inTransaction(final TransactionIsolationLevel isolation, final TransactionConsumer callback) throws CallbackFailedException
{
withHandle(new HandleConsumer() {
@Override
public void withHandle(Handle handle) throws Exception
{
handle.inTransaction(isolation, callback);
}
});
}

/**
* Open a handle and attach a new sql object of the specified type to that handle. Be sure to close the
* sql object (via a close() method, or calling {@link IDBI#close(Object)}
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/skife/jdbi/v2/Handle.java
Expand Up @@ -145,6 +145,15 @@ public interface Handle extends Closeable
*/
<ReturnType> ReturnType inTransaction(TransactionCallback<ReturnType> callback) throws TransactionFailedException;

/**
* Executes <code>callback</code> in a transaction. If the transaction succeeds, the
* result of the callback will be returned. If it fails a {@link TransactionFailedException}
* will be thrown.
*
* @throws TransactionFailedException if the transaction failed in the callback
*/
void inTransaction(TransactionConsumer callback) throws TransactionFailedException;

/**
* Executes <code>callback</code> in a transaction. If the transaction succeeds, the
* result of the callback will be returned. If it fails a {@link TransactionFailedException}
Expand All @@ -159,6 +168,19 @@ public interface Handle extends Closeable
<ReturnType> ReturnType inTransaction(TransactionIsolationLevel level,
TransactionCallback<ReturnType> callback) throws TransactionFailedException;

/**
* Executes <code>callback</code> in a transaction. If the transaction succeeds, the
* result of the callback will be returned. If it fails a {@link TransactionFailedException}
* will be thrown.
* <p>
* This form accepts a transaction isolation level which will be applied to the connection
* for the scope of this transaction, after which the original isolation level will be restored.
* </p>
* @return value returned from the callback
* @throws TransactionFailedException if the transaction failed in the callback
*/
void inTransaction(TransactionIsolationLevel level, TransactionConsumer callback) throws TransactionFailedException;

/**
* Convenience method which executes a select with purely positional arguments
* @param sql SQL or named statement
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/org/skife/jdbi/v2/IDBI.java
Expand Up @@ -17,6 +17,9 @@

import org.skife.jdbi.v2.exceptions.CallbackFailedException;
import org.skife.jdbi.v2.tweak.HandleCallback;
import org.skife.jdbi.v2.tweak.HandleConsumer;

import java.util.concurrent.Callable;

/**
* An interface for {@link DBI} instances for systems which like
Expand Down Expand Up @@ -55,6 +58,19 @@ public interface IDBI
*/
<ReturnType> ReturnType withHandle(HandleCallback<ReturnType> callback) throws CallbackFailedException;

/**
* A convenience function which manages the lifecycle of a handle and yields it to a callback
* for use by clients.
*
* @param callback A callback which will receive an open Handle
*
* @return the value returned by callback
*
* @throws CallbackFailedException Will be thrown if callback raises an exception. This exception will
* wrap the exception thrown by the callback.
*/
void withHandle(HandleConsumer callback) throws CallbackFailedException;

/**
* A convenience function which manages the lifecycle of a handle and yields it to a callback
* for use by clients. The handle will be in a transaction when the callback is invoked, and
Expand All @@ -70,6 +86,21 @@ public interface IDBI
*/
<ReturnType> ReturnType inTransaction(TransactionCallback<ReturnType> callback) throws CallbackFailedException;

/**
* A convenience function which manages the lifecycle of a handle and yields it to a callback
* for use by clients. The handle will be in a transaction when the callback is invoked, and
* that transaction will be committed if the callback finishes normally, or rolled back if the
* callback raises an exception.
*
* @param callback A callback which will receive an open Handle, in a transaction
*
* @return the value returned by callback
*
* @throws CallbackFailedException Will be thrown if callback raises an exception. This exception will
* wrap the exception thrown by the callback.
*/
void inTransaction(TransactionConsumer callback) throws CallbackFailedException;

/**
* A convenience function which manages the lifecycle of a handle and yields it to a callback
* for use by clients. The handle will be in a transaction when the callback is invoked, and
Expand All @@ -86,6 +117,22 @@ public interface IDBI
*/
<ReturnType> ReturnType inTransaction(TransactionIsolationLevel isolation, TransactionCallback<ReturnType> callback) throws CallbackFailedException;

/**
* A convenience function which manages the lifecycle of a handle and yields it to a callback
* for use by clients. The handle will be in a transaction when the callback is invoked, and
* that transaction will be committed if the callback finishes normally, or rolled back if the
* callback raises an exception.
*
* @param isolation The transaction isolation level to set
* @param callback A callback which will receive an open Handle, in a transaction
*
* @return the value returned by callback
*
* @throws CallbackFailedException Will be thrown if callback raises an exception. This exception will
* wrap the exception thrown by the callback.
*/
void inTransaction(TransactionIsolationLevel isolation, TransactionConsumer callback) throws CallbackFailedException;

/**
* Open a handle and attach a new sql object of the specified type to that handle. Be sure to close the
* sql object (via a close() method, or calling {@link IDBI#close(Object)}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/skife/jdbi/v2/TransactionConsumer.java
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2004 - 2014 Brian McCallister
*
* 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.
*/
package org.skife.jdbi.v2;

/**
* Used as a callback which guarantees that the inTransaction method is invoked in
* a transaction, and will be committed or rolled back as specified.
*/
public interface TransactionConsumer
{
void inTransaction(Handle conn, TransactionStatus status) throws Exception;
}
35 changes: 35 additions & 0 deletions src/main/java/org/skife/jdbi/v2/tweak/HandleConsumer.java
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2004 - 2014 Brian McCallister
*
* 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.
*/
package org.skife.jdbi.v2.tweak;

import org.skife.jdbi.v2.Handle;

/**
* Callback for use with {@link org.skife.jdbi.v2.DBI#withHandle(HandleConsumer)}
*/
public interface HandleConsumer
{
/**
* Will be invoked with an open Handle. The handle will be closed when this
* callback returns. Any exception thrown will be wrapped in a
* {@link org.skife.jdbi.v2.exceptions.CallbackFailedException}
*
* @param handle Handle to be used only within scope of this callback
* @throws Exception will result in a {@link org.skife.jdbi.v2.exceptions.CallbackFailedException} wrapping
* the exception being thrown
*/
void withHandle(Handle handle) throws Exception;
}
Expand Up @@ -26,7 +26,7 @@

/**
* Handler designed to behave properly in a J2EE CMT environment. It will never
* explicitely begin or commit a transaction, and will throw a runtime exception
* explicitly begin or commit a transaction, and will throw a runtime exception
* when rollback is called to force rollback.
*/
public class CMTTransactionHandler implements TransactionHandler
Expand Down

0 comments on commit 70b4146

Please sign in to comment.