C:/Users/vincent/Data/Perso/dev/ocilib/ocilib/src/transaction.c

00001 /*
00002    +----------------------------------------------------------------------+   
00003    |                                                                      |
00004    |                     OCILIB - C Driver for Oracle                     |
00005    |                                                                      |
00006    |                      (C Wrapper for Oracle OCI)                      |
00007    |                                                                      |
00008    +----------------------------------------------------------------------+
00009    |                      Website : http://www.ocilib.net                 |
00010    +----------------------------------------------------------------------+
00011    |               Copyright (c) 2007-2009 Vincent ROGIER                 |
00012    +----------------------------------------------------------------------+
00013    | This library is free software; you can redistribute it and/or        |
00014    | modify it under the terms of the GNU Lesser General Public           |
00015    | License as published by the Free Software Foundation; either         |
00016    | version 2 of the License, or (at your option) any later version.     |
00017    |                                                                      |
00018    | This library is distributed in the hope that it will be useful,      |
00019    | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
00020    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
00021    | Lesser General Public License for more details.                      |
00022    |                                                                      |
00023    | You should have received a copy of the GNU Lesser General Public     |
00024    | License along with this library; if not, write to the Free           |
00025    | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   |
00026    +----------------------------------------------------------------------+
00027    |          Author: Vincent ROGIER <vince.rogier@gmail.com>             |
00028    +----------------------------------------------------------------------+ 
00029 */
00030 
00031 /* ------------------------------------------------------------------------ *
00032  * $Id: transaction.c, v 3.4.0 2009-07-30 17:40 Vince $
00033  * ------------------------------------------------------------------------ */
00034 
00035 #include "ocilib_internal.h"
00036 
00037 /* ************************************************************************ *
00038  *                            PUBLIC FUNCTIONS
00039  * ************************************************************************ */
00040 
00041 /* ------------------------------------------------------------------------ *
00042  * OCI_TransactionCreate
00043  * ------------------------------------------------------------------------ */
00044 
00045 OCI_Transaction * OCI_API OCI_TransactionCreate(OCI_Connection *con,
00046                                                 unsigned int timeout, 
00047                                                 unsigned int mode,
00048                                                 OCI_XID *pxid)
00049 {
00050     OCI_Item *item         = NULL;
00051     OCI_Transaction *trans = NULL;
00052     boolean res            = TRUE;
00053 
00054     OCI_CHECK_INITIALIZED(NULL);
00055 
00056     OCI_CHECK_PTR(OCI_IPC_CONNECTION, con, NULL);
00057 
00058     /* create transaction object */
00059 
00060     item = OCI_ListAppend(con->trsns, sizeof(*trans));
00061  
00062     if (item != NULL)
00063     {
00064         trans = (OCI_Transaction *) item->data;
00065 
00066         trans->con      = con;
00067         trans->mode     = mode;
00068         trans->timeout  = timeout;
00069         trans->local    = (pxid == NULL);
00070 
00071         /* allocate transaction handle */
00072 
00073         if (res == TRUE)
00074             res = (OCI_SUCCESS == OCI_HandleAlloc((dvoid *) OCILib.env,
00075                                                   (dvoid **) (void *) &trans->htr,
00076                                                   (ub4) OCI_HTYPE_TRANS, 
00077                                                   (size_t) 0, (dvoid **) NULL));
00078 
00079         /* set context transaction attribute */
00080 
00081         OCI_CALL2
00082         (
00083             res, con, 
00084             
00085             OCIAttrSet((dvoid *) trans->con->cxt, (ub4) OCI_HTYPE_SVCCTX,
00086                        (dvoid *) trans->htr, (ub4) sizeof(trans->htr),
00087                        (ub4) OCI_ATTR_TRANS, trans->con->err)
00088         )
00089 
00090         /* set XID attribute for global transaction */
00091 
00092         if (pxid != NULL)
00093         {
00094             memcpy(&trans->xid , pxid, sizeof(trans->xid));
00095 
00096             OCI_CALL2
00097             (
00098                 res, con, 
00099                 
00100                 OCIAttrSet((dvoid *) trans->htr, (ub4) OCI_HTYPE_TRANS,
00101                            (dvoid *) &trans->xid, (ub4) sizeof(trans->xid),
00102                            (ub4) OCI_ATTR_XID, trans->con->err)
00103             )
00104         }
00105     }
00106     else
00107         res = FALSE;
00108 
00109    /* handle errors */
00110 
00111     if (res == FALSE)
00112     {
00113         OCI_TransactionFree(trans);
00114         trans = NULL;
00115     }
00116 
00117     OCI_RESULT(res);
00118 
00119     return trans;
00120 }
00121 
00122 /* ------------------------------------------------------------------------ *
00123  * OCI_TransactionClose
00124  * ------------------------------------------------------------------------ */
00125 
00126 boolean OCI_TransactionClose(OCI_Transaction * trans)
00127 {
00128     boolean res = TRUE;
00129 
00130     OCI_CHECK_PTR(OCI_IPC_TRANSACTION, trans, FALSE);
00131 
00132     res = OCI_TransactionStop(trans);
00133 
00134     /* close transaction handle */
00135 
00136     if (trans->htr != NULL)
00137     {
00138         OCI_HandleFree((dvoid *) trans->htr, (ub4) OCI_HTYPE_TRANS);
00139     }
00140 
00141     return res;
00142 }
00143 
00144 /* ------------------------------------------------------------------------ *
00145  * OCI_TransactionFree
00146  * ------------------------------------------------------------------------ */
00147 
00148 boolean OCI_API OCI_TransactionFree(OCI_Transaction * trans)
00149 {
00150     boolean res = TRUE;
00151 
00152     OCI_CHECK_PTR(OCI_IPC_TRANSACTION, trans, FALSE);
00153 
00154     res = OCI_TransactionClose(trans);
00155 
00156     /* remove transaction from internal list */
00157 
00158     OCI_ListRemove(trans->con->trsns, trans);
00159 
00160     OCI_FREE(trans);
00161 
00162     OCI_RESULT(res);
00163 
00164     return res;
00165 }
00166 
00167 /* ------------------------------------------------------------------------ *
00168  * OCI_TransactionStart
00169  * ------------------------------------------------------------------------ */
00170 
00171 boolean OCI_API OCI_TransactionStart(OCI_Transaction * trans)
00172 {
00173     boolean res = TRUE;
00174 
00175     OCI_CHECK_PTR(OCI_IPC_TRANSACTION, trans, FALSE);
00176 
00177     OCI_CALL2
00178     (
00179         res, trans->con, 
00180         
00181         OCITransStart(trans->con->cxt, trans->con->err, (uword) trans->timeout, 
00182                       (ub4) trans->mode)
00183     )
00184 
00185     OCI_RESULT(res);
00186 
00187     return res;
00188 }
00189 
00190 /* ------------------------------------------------------------------------ *
00191  * OCI_TransactionStop
00192  * ------------------------------------------------------------------------ */
00193 
00194 boolean OCI_API OCI_TransactionStop(OCI_Transaction * trans)
00195 {
00196     boolean res = TRUE;
00197 
00198     OCI_CHECK_PTR(OCI_IPC_TRANSACTION, trans, FALSE);
00199 
00200     /* commit or rollback upon auto commit mode */
00201 
00202     if (trans->con->autocom == TRUE)
00203         res = OCI_Commit(trans->con);
00204     else
00205         res = OCI_Rollback(trans->con);
00206 
00207     /* detach global transaction */
00208 
00209     if (trans->local == FALSE)
00210     {
00211         OCI_CALL2
00212         (
00213             res, trans->con, 
00214             
00215             OCITransDetach(trans->con->cxt, trans->con->err, (ub4) OCI_DEFAULT)
00216         )
00217     }
00218 
00219     OCI_RESULT(res);
00220 
00221     return res;
00222 }
00223 
00224 /* ------------------------------------------------------------------------ *
00225  * OCI_TransactionResume
00226  * ------------------------------------------------------------------------ */
00227 
00228 boolean OCI_API OCI_TransactionResume(OCI_Transaction * trans)
00229 {
00230     boolean res = TRUE;
00231 
00232     OCI_CHECK_PTR(OCI_IPC_TRANSACTION, trans, FALSE);
00233 
00234     OCI_CALL2
00235     (
00236         res, trans->con, 
00237         
00238         OCITransStart(trans->con->cxt, trans->con->err,
00239                       (uword) trans->timeout, (ub4) OCI_TRANS_RESUME)
00240     )
00241 
00242     OCI_RESULT(res);
00243 
00244     return res;
00245 }
00246 
00247 /* ------------------------------------------------------------------------ *
00248  * OCI_TransactionPrepare
00249  * ------------------------------------------------------------------------ */
00250 
00251 boolean OCI_API OCI_TransactionPrepare(OCI_Transaction * trans)
00252 {
00253     boolean res = TRUE;
00254 
00255     OCI_CHECK_PTR(OCI_IPC_TRANSACTION, trans, FALSE);
00256 
00257     OCI_CALL2
00258     (
00259         res, trans->con, 
00260         
00261         OCITransPrepare(trans->con->cxt, trans->con->err, (ub4) OCI_DEFAULT)
00262     )
00263 
00264     OCI_RESULT(res);
00265 
00266     return res;
00267 }
00268 
00269 /* ------------------------------------------------------------------------ *
00270  * OCI_TransactionForget
00271  * ------------------------------------------------------------------------ */
00272 
00273 boolean OCI_API OCI_TransactionForget(OCI_Transaction * trans)
00274 {
00275     boolean res = TRUE;
00276 
00277     OCI_CHECK_PTR(OCI_IPC_TRANSACTION, trans, FALSE);
00278 
00279     OCI_CALL2
00280     (
00281         res, trans->con, 
00282         
00283         OCITransForget(trans->con->cxt, trans->con->err, (ub4) OCI_DEFAULT)
00284     )
00285 
00286     OCI_RESULT(res);
00287 
00288     return res;
00289 }
00290 
00291 /* ------------------------------------------------------------------------ *
00292  * OCI_TransactionGetMode
00293  * ------------------------------------------------------------------------ */
00294 
00295 unsigned int OCI_API OCI_TransactionGetMode(OCI_Transaction * trans)
00296 {
00297     OCI_CHECK_PTR(OCI_IPC_TRANSACTION, trans, OCI_UNKNOWN);
00298 
00299     OCI_RESULT(TRUE);
00300 
00301     return trans->mode;
00302 }
00303 
00304 /* ------------------------------------------------------------------------ *
00305  * OCI_TransactionGetTimeout
00306  * ------------------------------------------------------------------------ */
00307 
00308 unsigned int OCI_API OCI_TransactionGetTimeout(OCI_Transaction * trans)
00309 {
00310     OCI_CHECK_PTR(OCI_IPC_TRANSACTION, trans, 0);
00311 
00312     OCI_RESULT(TRUE);
00313 
00314     return trans->timeout;
00315 }

Generated on Thu Jul 30 17:41:53 2009 for OCILIB (C Driver for Oracle) by  doxygen 1.5.4