Oracle
 sql >> Datenbank >  >> RDS >> Oracle

Spring JDBC-Verbindungspool und InputStream-Ergebnisse

Leider ging meiner Fantasie freien Lauf, als Sie diese Frage stellten. Ich weiß nicht, ob diese Lösung als eleganter angesehen wird. Diese Klassen sind jedoch einfach und leicht wiederverwendbar, sodass Sie sie möglicherweise verwenden können, wenn sie nicht zufriedenstellend sind. Sie werden sehen, wie sich am Ende alles zusammenfügt...

public class BinaryCloseable implements Closeable {

    private Closeable first;
    private Closeable last;

    public BinaryCloseable(Closeable first, Closeable last) {
        this.first = first;
        this.last = last;
    }

    @Override
    public void close() throws IOException {
        try {
            first.close();
        } finally {
            last.close();
        }
    }

}

BinaryCloseable wird von CompositeCloseable verwendet :

public class CompositeCloseable implements Closeable {

    private Closeable target;

    public CompositeCloseable(Closeable... closeables) {
        target = new Closeable() { public void close(){} };
        for (Closeable closeable : closeables) {
            target = new BinaryCloseable(target, closeable);
        }
    }

    @Override
    public void close() throws IOException {
        target.close();
    }

}

Der ResultSetCloser schließt ResultSet Objekte:

public class ResultSetCloser implements Closeable {

    private ResultSet resultSet;

    public ResultSetCloser(ResultSet resultSet) {
        this.resultSet = resultSet;
    }

    @Override
    public void close() throws IOException {
        try {
            resultSet.close();
        } catch (SQLException e) {
            throw new IOException("Exception encountered while closing result set", e);
        }
    }

}

Der PreparedStatementCloser schließt PreparedStatement Objekte:

public class PreparedStatementCloser implements Closeable {

    private PreparedStatement preparedStatement;

    public PreparedStatementCloser(PreparedStatement preparedStatement) {
        this.preparedStatement = preparedStatement;
    }

    @Override
    public void close() throws IOException {
        try {
            preparedStatement.close();
        } catch (SQLException e) {
            throw new IOException("Exception encountered while closing prepared statement", e);
        }
    }

}

Der ConnectionCloser schließt Connection Objekte:

public class ConnectionCloser implements Closeable {

    private Connection connection;

    public ConnectionCloser(Connection connection) {
        this.connection = connection;
    }

    @Override
    public void close() throws IOException {
        try {
            connection.close();
        } catch (SQLException e) {
            throw new IOException("Exception encountered while closing connection", e);
        }
    }

}

Wir überarbeiten jetzt Ihren ursprünglichen InputStream Idee in:

public class ClosingInputStream extends InputStream {

    private InputStream stream;
    private Closeable closer;

    public ClosingInputStream(InputStream stream, Closeable closer) {
        this.stream = stream;
        this.closer = closer;
    }

    // The other InputStream methods...

    @Override
    public void close() throws IOException {
        closer.close();
    }

}

Schließlich kommt alles zusammen als:

new ClosingInputStream(
        stream,
        new CompositeCloseable(
                stream,
                new ResultSetCloser(resultSet),
                new PreparedStatementCloser(statement),
                new ConnectionCloser(connection)
            )
    );

Wenn dieser ClosingInputStream 's close() -Methode aufgerufen wird, geschieht effektiv Folgendes (wobei die Behandlung von Ausnahmen der Übersichtlichkeit halber weggelassen wurde):

public void close() {
    try {
        try {
            try {
                try {
                    // This is empty due to the first line in `CompositeCloseable`'s constructor
                } finally {
                    stream.close();
                }
            } finally {
                resultSet.close();
            }
        } finally {
            preparedStatement.close();
        }
    } finally {
        connection.close();
    }
}

Sie können jetzt beliebig viele Closeable schließen Objekte, wie Sie möchten.