001    /*
002     * Copyright (c) 2009 The openGion Project.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013     * either express or implied. See the License for the specific language
014     * governing permissions and limitations under the License.
015     */
016    package org.opengion.fukurou.transfer;
017    
018    import java.sql.CallableStatement;
019    import java.sql.Connection;
020    import java.sql.SQLException;
021    import java.sql.Types;
022    
023    import org.opengion.fukurou.db.Transaction;
024    import org.opengion.fukurou.util.ErrorMessage;
025    import org.opengion.fukurou.util.Closer;
026    
027    /**
028     * 伝?要求に対してのPL/SQLを実行します?
029     *
030     * 実行するPL/SQLのPGIDは、実行対象で?します?
031     * ?形式?、PG? ?, ?, ... )  ? GEP00002.GEP00002( ?,?,0 ) です?
032     * 引数は、従来のPL/SQLの実行が可能なように、第?数はエラーコード?第二引数は?
033     * エラーメ?ージを返してきます?第三引数以降?、?由に?できます?
034     *
035     * @og.group 伝?シス?
036     *
037     * @version  5.0
038     * @author   Hiroki.Nakamura
039     * @since    JDK1.6
040     */
041    public class TransferExec_PLSQL implements TransferExec {
042    
043            private static final int DB_MAX_QUERY_TIMEOUT = 6000;
044    
045            /**
046             * PL/SQLを実行します?
047             *
048             * @og.rev 5.5.2.4 (2012/05/16) クローズされな?路の処?
049             *
050             * @param vals 伝???タ(配?)
051             * @param config 伝?設定オブジェク?
052             * @param tran トランザクションオブジェク?
053             */
054            @Override
055            public void execute( final String[] vals, final TransferConfig config, final Transaction tran ) {
056                    CallableStatement callStmt = null;
057                    try {
058                            String stmt = "{ call " + config.getExecObj() + "}";
059    
060                            Connection conn = tran.getConnection( config.getExecDbid() );
061    //                      CallableStatement callStmt = conn.prepareCall( stmt );
062                            callStmt = conn.prepareCall( stmt );                                            // 5.5.2.4 (2012/05/16)
063                            callStmt.setQueryTimeout( DB_MAX_QUERY_TIMEOUT );
064                            callStmt.registerOutParameter(1, Types.INTEGER);
065                            callStmt.registerOutParameter(2, Types.VARCHAR);
066                            callStmt.execute();
067    
068                            int errCode = callStmt.getInt(1);
069                            String errMsg = callStmt.getString(2);
070    
071                            if( errCode == ErrorMessage.OK ) { // 正常終??場?
072                                    tran.commit();
073                            }
074                            else {
075                                    tran.rollback();
076                                    errMsg = "PL/SQL実行時に例外が発生しました? + errMsg;
077                                    throw new RuntimeException( errMsg );
078                            }
079                    }
080                    catch( SQLException ex ) {
081                            tran.rollback();
082                            String errMsg = "PL/SQL呼出時に例外が発生しました?;
083                            throw new RuntimeException( errMsg, ex );
084                    }
085                    // 5.5.2.4 (2012/05/16)
086                    finally {
087                            Closer.stmtClose( callStmt );
088                    }
089            }
090    }