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 */
016package org.opengion.fukurou.transfer;
017
018import java.sql.CallableStatement;
019import java.sql.Connection;
020import java.sql.SQLException;
021import java.sql.Types;
022
023import org.opengion.fukurou.db.Transaction;
024import org.opengion.fukurou.util.ErrorMessage;
025import 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 */
041public 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}