package org.skife.jdbi.v2.sqlobject.customizers;

import org.skife.jdbi.v2.SQLStatement;
import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.TransactionIsolationLevel;
import org.skife.jdbi.v2.sqlobject.SqlStatementCustomizer;
import org.skife.jdbi.v2.sqlobject.SqlStatementCustomizerFactory;
import org.skife.jdbi.v2.sqlobject.SqlStatementCustomizingAnnotation;
import org.skife.jdbi.v2.tweak.BaseStatementCustomizer;

import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Used to specify the transaction isolation level for an object or method (via annotating the method
 * or passing it in as an annotated param). If used on a parameter, the parameter type must be a
 * {@link org.skife.jdbi.v2.TransactionIsolationLevel}
 */
@SqlStatementCustomizingAnnotation(TransactionIsolation.Factory.class)
@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface TransactionIsolation
{

    TransactionIsolationLevel value() default TransactionIsolationLevel.INVALID_LEVEL;

    static class Factory implements SqlStatementCustomizerFactory
    {

        public SqlStatementCustomizer createForMethod(Annotation annotation, Class sqlObjectType, Method method)
        {
            return new MyCustomizer(((TransactionIsolation) annotation).value());
        }

        public SqlStatementCustomizer createForType(Annotation annotation, Class sqlObjectType)
        {
            return new MyCustomizer(((TransactionIsolation) annotation).value());
        }

        public SqlStatementCustomizer createForParameter(Annotation annotation, Class sqlObjectType, Method method, Object arg)
        {
            assert arg instanceof TransactionIsolationLevel;
            return new MyCustomizer((TransactionIsolationLevel) arg);
        }
    }

    static class MyCustomizer implements SqlStatementCustomizer
    {

        private final TransactionIsolationLevel level;

        public MyCustomizer(TransactionIsolationLevel level) {this.level = level;}

        public void apply(SQLStatement q) throws SQLException
        {
            final int initial_level = q.getContext().getConnection().getTransactionIsolation();

            q.addStatementCustomizer(new BaseStatementCustomizer()
            {
                @Override
                public void beforeExecution(PreparedStatement stmt, StatementContext ctx) throws SQLException
                {
                    ctx.getConnection().setTransactionIsolation(level.intValue());
                }

                @Override
                public void afterExecution(PreparedStatement stmt, StatementContext ctx) throws SQLException
                {
                    ctx.getConnection().setTransactionIsolation(initial_level);
                }
            });
        }
    }
}

@@ -663,16 +663,44 @@
 Method;%0A
+import java.sql.Connection;%0A
 import j
@@ -2740,41 +2740,13 @@
     
-ctx.getConnection().setTransactio
+setTx
 nIso
@@ -2752,16 +2752,21 @@
 olation(
+ctx, 
 level.in
@@ -2972,67 +2972,401 @@
     
-ctx.getConnection().setTransactionIsolation(initial_level);
+setTxnIsolation(ctx, initial_level);%0A                %7D%0A                %0A                private void setTxnIsolation(StatementContext ctx, int level) throws SQLException%0A                %7B%0A                    final Connection c = ctx.getConnection();%0A                    if (c.getTransactionIsolation() != level) %7B%0A                        c.setTransactionIsolation(level);%0A                    %7D
 %0A   

Change the TransactionIsolation customizer to not attempt to set the transaction isolation level if it is already set. This is an error in some JDBC drivers.