KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jdbc > standard > StandardXACallableStatement


1 /*
2  * XAPool: Open Source XA JDBC Pool
3  * Copyright (C) 2003 Objectweb.org
4  * Initial Developer: Lutris Technologies Inc.
5  * Contact: xapool-public@lists.debian-sf.objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA
21  */

22 package org.enhydra.jdbc.standard;
23
24 import java.sql.SQLException JavaDoc;
25 import java.sql.CallableStatement JavaDoc;
26 import org.enhydra.jdbc.core.CoreCallableStatement;
27 import javax.transaction.Transaction JavaDoc;
28 import javax.transaction.SystemException JavaDoc;
29 import javax.transaction.RollbackException JavaDoc;
30
31 public class StandardXACallableStatement extends CoreCallableStatement {
32
33     private StandardXAConnectionHandle con;
34     // the StandardConnectionHandle that created this object
35
private boolean closed; // true when the Statement has been closed
36
private String JavaDoc sql;
37     private int resultSetType;
38     private int resultSetConcurrency;
39         private int resultSetHoldability;
40
41     /**
42      * Constructor.
43      */

44     StandardXACallableStatement(
45         StandardXAConnectionHandle con,
46         String JavaDoc sql,
47         int resultSetType,
48         int resultSetConcurrency,
49         int resultSetHoldability)
50         throws SQLException JavaDoc {
51         this.con = con;
52         this.sql = sql;
53         this.resultSetType = resultSetType;
54         this.resultSetConcurrency = resultSetConcurrency;
55         this.resultSetHoldability = resultSetHoldability;
56         log = con.log;
57         //cs = newStatement();
58
}
59
60     private CallableStatement JavaDoc newStatement() throws SQLException JavaDoc {
61         if (resultSetType == 0 && resultSetConcurrency == 0 && resultSetHoldability == 0) {
62             return con.con.prepareCall(sql);
63         } else if (resultSetHoldability == 0) {
64             return con.con.prepareCall(
65                 sql,
66                 resultSetType,
67                 resultSetConcurrency);
68         } else return con.con.prepareCall(
69                           sql,
70                           resultSetType,
71                           resultSetConcurrency,
72                           resultSetHoldability);
73         
74
75     }
76
77     /**
78      * Close this statement.
79      */

80     public synchronized void close() throws SQLException JavaDoc {
81         super.close(); // we do not reuse the Statement, we have to close it
82
closed = true;
83     }
84
85     /**
86      * Pre-invokation of the delegation, in case of the Statement is
87      * closed, we throw an exception
88      */

89     public synchronized void preInvoke() throws SQLException JavaDoc {
90         if (closed)
91             throw new SQLException JavaDoc("Prepare Statement is closed");
92
93         Transaction JavaDoc ntx = null;
94         if (con.tx == null) {
95             try {
96                 try {
97                     ntx =
98                         (con.transactionManager != null)
99                             ? con.transactionManager.getTransaction()
100                             : null;
101                     if (ntx != null) {
102                         con.tx = ntx;
103                         con.xacon.thisAutoCommit = con.getAutoCommit();
104                         con.setAutoCommit(false);
105                         try {
106                             con.tx.enlistResource(con.xacon.getXAResource());
107                             // enlist the xaResource in the transaction
108
if (cs != null) {
109                                 cs.close();
110                                 cs = null;
111                             }
112                         } catch (RollbackException JavaDoc n) {
113                             throw new SQLException JavaDoc(
114                                 "StandardXAStatement:preInvoke enlistResource exception: "
115                                     + n.toString());
116                         }
117                     }
118                     //else con.setAutoCommit(true);
119

120                 } catch (SystemException JavaDoc n) {
121                     throw new SQLException JavaDoc(
122                         "StandardXAStatement:preInvoke getTransaction exception: "
123                             + n.toString());
124                 }
125             } catch (NullPointerException JavaDoc n) {
126                 // current is null: we are not in EJBServer.
127
throw new SQLException JavaDoc(
128                     "StandardXAStatement:preInvoke should not be used outside an EJBServer: "
129                         + n.toString());
130             }
131         }
132         if (cs == null) {
133             cs = newStatement();
134         }
135
136     }
137
138     /**
139      * Exception management : catch or throw the exception
140      */

141     public void catchInvoke(SQLException JavaDoc sqlException) throws SQLException JavaDoc {
142         //ConnectionEvent event = new ConnectionEvent (con.pooledCon);
143
//con.pooledCon.connectionErrorOccurred(event);
144
throw (sqlException);
145     }
146
147 }
148
Popular Tags