-Поиск по дневнику

Поиск сообщений в rss_thedaily_wtf

 -Подписка по e-mail

 

 -Постоянные читатели

 -Статистика

Статистика LiveInternet.ru: показано количество хитов и посетителей
Создан: 06.04.2008
Записей:
Комментариев:
Написано: 0


CodeSOD: Prepared for the Real World

Среда, 29 Марта 2017 г. 13:30 + в цитатник

Usul is taking a college course on Java programming, and its doing an excellent job preparing him for the real world. Already, hes been forced to cope with someone who knows one true fact and has run off to apply it in the dumbest way possible. This would be his professor.

Our true fact is this: A Java PreparedStament object, used for running database queries, should be closed after use. This returns its connection to the pool and frees any resources the statement was using. You should do this, and you should do it as soon as youre done with your connection.

Now, putting a call to stmt.close() in every finally block was just too much for Professor McCloseypants to deal with. So he provided this convenience object to deal with that problem.

Its a lot of code, so were going to do this in pieces. First, lets look at the declaration:

public class ClosingPreparedStatement {
    …
}

Now, remember, the purpose of this class is to do everything a PreparedStatement does, but add a self-closing behavior. Thus, the first problem here is that it doesnt have an extends or implements declaration, so that it could share a common interface with PreparedStatements.

        /**
         *
         */
        public static boolean somethingFinalized = false;

Remember, this is a professor, providing this code as a model of what his students should be doing. For the capstone course, required for graduation.

Now, this is a wrapper around a PreparedStatement, so guess how most of the methods work…

        /**
         * Just delegates
         *
         * @param arg0 delegate's parameter
         * @param arg1 delegate's parameter
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public int executeUpdate(String arg0, String[] arg1) throws SQLException
        {
                return stmt.executeUpdate(arg0, arg1);
        }

Oh yeah. There are almost 1,300 lines of code in this file, and yes, theyre pretty much all like this. So, with all of that, how does this magical self-closing statement work?

        /**
         * Closes the internal statement and delegates
         *
         * @throws Throwable if the delegate throws it
         */
        protected void finalize() throws Throwable
        {
                try
                {
                        stmt.close();
                        somethingFinalized = true;
                } finally
                {
                        super.finalize();
                }
        }

He put it in the finalize method. If youre not intimate with Javas memory management, keep in mind: the finalize method is called by the garbage collector when it finally cleans up memory. You have no guarantees about when that will happen, or even if it ever well. So much for the as soon as youre done.

Here is all 1,300ish lines of the file, in all its delegatory glory:

package datasource;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.Date;
import java.sql.NClob;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;

/**
 * This Prepared statement will always finalized itself to make sure that
 * everything gets cleaned up.
 */
public class ClosingPreparedStatement
{

        /**
         *
         */
        public static boolean somethingFinalized = false;

        private PreparedStatement stmt;

        /**
         * @param connection the connection
         * @param sql the sql for this statement
         * @throws SQLException if the prepared statement it contains throws an
         *             exception
         */
        public ClosingPreparedStatement(Connection connection, String sql) throws SQLException
        {
                stmt = connection.prepareStatement(sql);

        }

        /**
         *
         * @param connection the connection we should use
         * @param sql the statement we will execute
         * @param autoGeneratedKeys the keys
         * @throws SQLException if we can't create the statement
         */
        public ClosingPreparedStatement(Connection connection, String sql, int autoGeneratedKeys) throws SQLException
        {
                stmt = connection.prepareStatement(sql, autoGeneratedKeys);

        }

        /**
         * Just delegates
         *
         * @throws SQLException if delegated call throws it
         */
        public void addBatch() throws SQLException
        {
                stmt.addBatch();
        }

        /**
         * Just delegates
         *
         * @param arg0 delegate
         * @throws SQLException if delegated call throws it
         */
        public void addBatch(String arg0) throws SQLException
        {
                stmt.addBatch(arg0);
        }

        /**
         * Just delegates
         *
         * @throws SQLException if delegated call throws it
         */
        public void cancel() throws SQLException
        {
                stmt.cancel();
        }

        /**
         * Just delegates
         *
         * @throws SQLException if delegate throws it
         */
        public void clearBatch() throws SQLException
        {
                stmt.clearBatch();
        }

        /**
         * Just delegates
         *
         * @throws SQLException if delegate throws it
         */
        public void clearParameters() throws SQLException
        {
                stmt.clearParameters();
        }

        /**
         * Just delegates
         *
         * @throws SQLException if delegate throws it
         */
        public void clearWarnings() throws SQLException
        {
                stmt.clearWarnings();
        }

        /**
         * Just delegates
         *
         * @throws SQLException if delegate throws it
         */
        public void close() throws SQLException
        {
                stmt.close();
        }

        /**
         * Just delegates
         *
         * @throws SQLException if delegate throws it
         */
        public void closeOnCompletion() throws SQLException
        {
                stmt.closeOnCompletion();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public boolean execute() throws SQLException
        {
                return stmt.execute();
        }

        /**
         * Just delegates
         *
         * @param arg0 delegate's parameter
         * @return the delgate's return value
         * @throws SQLException if delegate throws it
         */
        public boolean execute(String arg0) throws SQLException
        {
                return stmt.execute(arg0);
        }

        /**
         * Just delegates
         *
         * @param arg0 delegate's parameter
         * @param arg1 delegate's parameter
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public boolean execute(String arg0, int arg1) throws SQLException
        {
                return stmt.execute(arg0, arg1);
        }

        /**
         * Just delegates
         *
         * @param arg0 delegate's parameter
         * @param arg1 delegate's parameter
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public boolean execute(String arg0, int[] arg1) throws SQLException
        {
                return stmt.execute(arg0, arg1);
        }

        /**
         * Just delegates
         *
         * @param arg0 delegate's parameter
         * @param arg1 delegate's parameter
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public boolean execute(String arg0, String[] arg1) throws SQLException
        {
                return stmt.execute(arg0, arg1);
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public int[] executeBatch() throws SQLException
        {
                return stmt.executeBatch();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public ResultSet executeQuery() throws SQLException
        {
                return stmt.executeQuery();
        }

        /**
         * Just delegates
         *
         * @param arg0 delegate's parameter
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public ResultSet executeQuery(String arg0) throws SQLException
        {
                return stmt.executeQuery(arg0);
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public int executeUpdate() throws SQLException
        {
                return stmt.executeUpdate();
        }

        /**
         * Just delegates
         *
         * @param arg0 delegate's parameter
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public int executeUpdate(String arg0) throws SQLException
        {
                return stmt.executeUpdate(arg0);
        }

        /**
         * Just delegates
         *
         * @param arg0 delegate's parameter
         * @param arg1 delegate's parameter
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public int executeUpdate(String arg0, int arg1) throws SQLException
        {
                return stmt.executeUpdate(arg0, arg1);
        }

        /**
         * Just delegates
         *
         * @param arg0 delegate's parameter
         * @param arg1 delegate's parameter
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public int executeUpdate(String arg0, int[] arg1) throws SQLException
        {
                return stmt.executeUpdate(arg0, arg1);
        }

        /**
         * Just delegates
         *
         * @param arg0 delegate's parameter
         * @param arg1 delegate's parameter
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public int executeUpdate(String arg0, String[] arg1) throws SQLException
        {
                return stmt.executeUpdate(arg0, arg1);
        }

        /**
         * Closes the internal statement and delegates
         *
         * @throws Throwable if the delegate throws it
         */
        protected void finalize() throws Throwable
        {
                try
                {
                        stmt.close();
                        somethingFinalized = true;
                } finally
                {
                        super.finalize();
                }
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public java.sql.Connection getConnection() throws SQLException
        {
                return stmt.getConnection();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public int getFetchDirection() throws SQLException
        {
                return stmt.getFetchDirection();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public int getFetchSize() throws SQLException
        {
                return stmt.getFetchSize();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public ResultSet getGeneratedKeys() throws SQLException
        {
                return stmt.getGeneratedKeys();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public int getMaxFieldSize() throws SQLException
        {
                return stmt.getMaxFieldSize();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public int getMaxRows() throws SQLException
        {
                return stmt.getMaxRows();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public ResultSetMetaData getMetaData() throws SQLException
        {
                return stmt.getMetaData();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public boolean getMoreResults() throws SQLException
        {
                return stmt.getMoreResults();
        }

        /**
         * Just delegates
         *
         * @param arg0 delegate's parameter
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public boolean getMoreResults(int arg0) throws SQLException
        {
                return stmt.getMoreResults(arg0);
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public ParameterMetaData getParameterMetaData() throws SQLException
        {
                return stmt.getParameterMetaData();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public int getQueryTimeout() throws SQLException
        {
                return stmt.getQueryTimeout();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public ResultSet getResultSet() throws SQLException
        {
                return stmt.getResultSet();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public int getResultSetConcurrency() throws SQLException
        {
                return stmt.getResultSetConcurrency();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public int getResultSetHoldability() throws SQLException
        {
                return stmt.getResultSetHoldability();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public int getResultSetType() throws SQLException
        {
                return stmt.getResultSetType();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public int getUpdateCount() throws SQLException
        {
                return stmt.getUpdateCount();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public SQLWarning getWarnings() throws SQLException
        {
                return stmt.getWarnings();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public boolean isClosed() throws SQLException
        {
                return stmt.isClosed();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public boolean isCloseOnCompletion() throws SQLException
        {
                return stmt.isCloseOnCompletion();
        }

        /**
         * Just delegates
         *
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public boolean isPoolable() throws SQLException
        {
                return stmt.isPoolable();
        }

        /**
         * Just delegates
         *
         * @param arg0 delegate's parameter
         * @return the delegates return value
         * @throws SQLException if delegate throws it
         */
        public boolean isWrapperFor(Class

http://thedailywtf.com/articles/prepared-for-the-real-world

Метки:  

 

Добавить комментарий:
Текст комментария: смайлики

Проверка орфографии: (найти ошибки)

Прикрепить картинку:

 Переводить URL в ссылку
 Подписаться на комментарии
 Подписать картинку