XTA: API Reference for C language

xta_postgresql_xa_resource.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2019, Christian Ferrari <tiian@users.sourceforge.net>
3  * All rights reserved.
4  *
5  * This file is part of LIXA.
6  *
7  * LIXA is free software: you can redistribute this file and/or modify
8  * it under the terms of the GNU Lesser General Public License version 2.1 as
9  * published by the Free Software Foundation.
10  *
11  * LIXA is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with LIXA. If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "config.h"
20 
21 
22 
23 #ifdef HAVE_ERRNO_H
24 # include <errno.h>
25 #endif
26 #ifdef HAVE_GLIB_H
27 # include <glib.h>
28 #endif
29 
30 
31 
32 /* LIXA includes */
33 #include "lixa_errors.h"
34 #include "lixa_trace.h"
35 #include "liblixapq.h" /* LIXA wrapper for PostgreSQL */
36 /* XTA includes */
38 
39 
40 
41 /* set module trace flag */
42 #ifdef LIXA_TRACE_MODULE
43 # undef LIXA_TRACE_MODULE
44 #endif /* LIXA_TRACE_MODULE */
45 #define LIXA_TRACE_MODULE LIXA_TRACE_MOD_XTA
46 
47 
48 
52 const static struct xta_iface_s xta_postgresql_iface = {
53  "XTA PostgreSQL",
54  TMNOFLAGS,
55  0,
65 };
66 
67 
68 
70  PGconn *connection, const char *name, const char *open_info)
71 {
72  enum Exception { G_TRY_MALLOC_ERROR
73  , XTA_POSTGRESQL_XA_RESOURCE_INIT_ERROR
74  , NONE } excp;
75  int ret_cod = LIXA_RC_INTERNAL_ERROR;
76  xta_postgresql_xa_resource_t *this = NULL;
77 
78  LIXA_TRACE(("xta_postgresql_xa_resource_new\n"));
79  TRY {
80  /* allocate sufficient memory for the object */
81  if (NULL == (this = (xta_postgresql_xa_resource_t *)g_try_malloc0(
83  THROW(G_TRY_MALLOC_ERROR);
84  /* initialize "class" properties */
86  this, connection, name, open_info)))
87  THROW(XTA_POSTGRESQL_XA_RESOURCE_INIT_ERROR);
88 
89  THROW(NONE);
90  } CATCH {
91  switch (excp) {
92  case G_TRY_MALLOC_ERROR:
94  break;
95  case XTA_POSTGRESQL_XA_RESOURCE_INIT_ERROR:
96  break;
97  case NONE:
98  ret_cod = LIXA_RC_OK;
99  break;
100  default:
101  ret_cod = LIXA_RC_INTERNAL_ERROR;
102  } /* switch (excp) */
103  } /* TRY-CATCH */
104  LIXA_TRACE(("xta_postgresql_xa_resource_new/excp=%d/"
105  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
106  return this;
107 }
108 
109 
110 
112  xta_postgresql_xa_resource_t *xa_resource)
113 {
114  enum Exception { NONE } excp;
115  int ret_cod = LIXA_RC_INTERNAL_ERROR;
116 
117  LIXA_TRACE(("xta_postgresql_xa_resource_delete\n"));
118  TRY {
119  /* clean the object before releasing */
121  /* release memory allocated for the object */
122  g_free(xa_resource);
123 
124  THROW(NONE);
125  } CATCH {
126  switch (excp) {
127  case NONE:
128  ret_cod = LIXA_RC_OK;
129  break;
130  default:
131  ret_cod = LIXA_RC_INTERNAL_ERROR;
132  } /* switch (excp) */
133  } /* TRY-CATCH */
134  LIXA_TRACE(("xta_postgresql_xa_resource_delete/excp=%d/"
135  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
136  return;
137 }
138 
139 
140 
142  xta_postgresql_xa_resource_t *xa_resource,
143  PGconn *connection, const char *name, const char *open_info)
144 {
145  enum Exception { XTA_ACQUIRED_XA_RESOURCE_INIT_ERROR
146  , NONE } excp;
147  int ret_cod = LIXA_RC_INTERNAL_ERROR;
148 
149  LIXA_TRACE(("xta_postgresql_xa_resource_init\n"));
150  TRY {
151  /* initialize "base class" (xta_acquired_xa_resource_t) properties */
152  if (LIXA_RC_OK != (ret_cod = xta_acquired_xa_resource_init(
153  (xta_acquired_xa_resource_t *)xa_resource,
154  &xta_postgresql_iface,
155  name, open_info)))
156  THROW(XTA_ACQUIRED_XA_RESOURCE_INIT_ERROR);
157  /* set connection */
158  xa_resource->connection = connection;
159  /* set resource interface */
161  &xa_resource->xa_resource.act_rsrmgr_config.lixa_iface,
162  &xta_postgresql_iface, (xta_xa_resource_t *)xa_resource);
163  /* reset status */
164  memset(&xa_resource->lssr, 0, sizeof(struct lixa_sw_status_rm_s));
165 
166  THROW(NONE);
167  } CATCH {
168  switch (excp) {
169  case XTA_ACQUIRED_XA_RESOURCE_INIT_ERROR:
170  break;
171  case NONE:
172  ret_cod = LIXA_RC_OK;
173  break;
174  default:
175  ret_cod = LIXA_RC_INTERNAL_ERROR;
176  } /* switch (excp) */
177  } /* TRY-CATCH */
178  LIXA_TRACE(("xta_postgresql_xa_resource_init/excp=%d/"
179  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
180  return ret_cod;
181 }
182 
183 
184 
186 {
187  enum Exception { NONE } excp;
188  int ret_cod = LIXA_RC_INTERNAL_ERROR;
189 
190  LIXA_TRACE(("xta_postgresql_xa_resource_clean\n"));
191  TRY {
192  /* clean PostgreSQL XA function pointers */
193  xa_resource->xa_resource.act_rsrmgr_config.lixa_iface.std = NULL;
194  /* clean "base class" (xta_acquired_xa_resource_t) properties */
196  (xta_acquired_xa_resource_t *)xa_resource);
197 
198  THROW(NONE);
199  } CATCH {
200  switch (excp) {
201  case NONE:
202  ret_cod = LIXA_RC_OK;
203  break;
204  default:
205  ret_cod = LIXA_RC_INTERNAL_ERROR;
206  } /* switch (excp) */
207  } /* TRY-CATCH */
208  LIXA_TRACE(("xta_postgresql_xa_resource_clean/excp=%d/"
209  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
210  return;
211 }
212 
213 
214 
215 int xta_postgresql_xa_open(xta_xa_resource_t *context, char *xa_info,
216  int rmid, long flags)
217 {
218  enum Exception { OBJ_CORRUPTED
219  , NONE } excp;
220  int ret_cod = XAER_RMERR;
221 
222  LIXA_TRACE(("xta_postgresql_xa_open\n"));
223  TRY {
225  (xta_postgresql_xa_resource_t *)context;
226  if (NULL != this->connection) {
227  LIXA_TRACE(("xta_postgresql_xa_open: PostgreSQL connection is "
228  "already open (%p)\n", this->connection));
229  } else {
230  LIXA_TRACE(("xta_postgresql_xa_open: PostgreSQL connection is "
231  "NULL!\n"));
232  THROW(OBJ_CORRUPTED);
233  }
234  /* save the connection state: backward compatibility with switch
235  * file based implemetation */
236  this->lssr.rmid = rmid;
237  this->lssr.rm_type = LIXA_SW_STATUS_RM_TYPE_POSTGRESQL;
238  this->lssr.state.R = 1;
239  this->lssr.conn = (gpointer)this->connection;
240 
241  THROW(NONE);
242  } CATCH {
243  switch (excp) {
244  case OBJ_CORRUPTED:
245  break;
246  case NONE:
247  ret_cod = XA_OK;
248  break;
249  default:
250  ret_cod = XAER_RMERR;
251  } /* switch (excp) */
252  } /* TRY-CATCH */
253  LIXA_TRACE(("xta_postgresql_xa_open/excp=%d/"
254  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
255  return ret_cod;
256 }
257 
258 
259 
260 int xta_postgresql_xa_close(xta_xa_resource_t *context, char *xa_info,
261  int rmid, long flags)
262 {
263  enum Exception { OBJ_CORRUPTED
264  , NONE } excp;
265  int ret_cod = XAER_RMERR;
266 
267  LIXA_TRACE(("xta_postgresql_xa_close\n"));
268  TRY {
270  (xta_postgresql_xa_resource_t *)context;
271  if (NULL != this->connection) {
272  LIXA_TRACE(("xta_postgresql_xa_close: PostgreSQL connection (%p) "
273  "must be closed by the Application Program\n",
274  this->connection));
275  } else {
276  LIXA_TRACE(("xta_postgresql_xa_close: PostgreSQL connection is "
277  "NULL!\n"));
278  THROW(OBJ_CORRUPTED);
279  }
280 
281  THROW(NONE);
282  } CATCH {
283  switch (excp) {
284  case OBJ_CORRUPTED:
285  break;
286  case NONE:
287  ret_cod = XA_OK;
288  break;
289  default:
290  ret_cod = XAER_RMERR;
291  } /* switch (excp) */
292  } /* TRY-CATCH */
293  LIXA_TRACE(("xta_postgresql_xa_close/excp=%d/"
294  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
295  return ret_cod;
296 }
297 
298 
299 
301  int rmid, long flags)
302 {
303  enum Exception { OBJ_CORRUPTED
304  , NONE } excp;
305  int ret_cod = XAER_RMERR;
306 
307  LIXA_TRACE(("xta_postgresql_xa_start\n"));
308  TRY {
310  (xta_postgresql_xa_resource_t *)context;
311  if (NULL == this->connection) {
312  LIXA_TRACE(("xta_postgresql_xa_start: PostgreSQL connection is "
313  "NULL!\n"));
314  THROW(OBJ_CORRUPTED);
315  }
316  /* call legacy switch file based code */
317  ret_cod = lixa_pq_start_core(&this->lssr, xid, rmid, flags);
318 
319  THROW(NONE);
320  } CATCH {
321  switch (excp) {
322  case OBJ_CORRUPTED:
323  break;
324  case NONE:
325  break;
326  default:
327  ret_cod = XAER_RMERR;
328  } /* switch (excp) */
329  } /* TRY-CATCH */
330  LIXA_TRACE(("xta_postgresql_xa_start/excp=%d/"
331  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
332  return ret_cod;
333 }
334 
335 
336 
337 int xta_postgresql_xa_end(xta_xa_resource_t *context, const XID *xid,
338  int rmid, long flags)
339 {
340  enum Exception { OBJ_CORRUPTED
341  , NONE } excp;
342  int ret_cod = XAER_RMERR;
343 
344  LIXA_TRACE(("xta_postgresql_xa_end\n"));
345  TRY {
347  (xta_postgresql_xa_resource_t *)context;
348  if (NULL == this->connection) {
349  LIXA_TRACE(("xta_postgresql_xa_end: PostgreSQL connection is "
350  "NULL!\n"));
351  THROW(OBJ_CORRUPTED);
352  }
353  /* call legacy switch file based code */
354  ret_cod = lixa_pq_end_core(&this->lssr, xid, rmid, flags);
355 
356  THROW(NONE);
357  } CATCH {
358  switch (excp) {
359  case OBJ_CORRUPTED:
360  break;
361  case NONE:
362  break;
363  default:
364  ret_cod = XAER_RMERR;
365  } /* switch (excp) */
366  } /* TRY-CATCH */
367  LIXA_TRACE(("xta_postgresql_xa_end/excp=%d/"
368  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
369  return ret_cod;
370 }
371 
372 
373 
374 
376  int rmid, long flags)
377 {
378  enum Exception { OBJ_CORRUPTED
379  , NONE } excp;
380  int ret_cod = XAER_RMERR;
381 
382  LIXA_TRACE(("xta_postgresql_xa_rollback\n"));
383  TRY {
385  (xta_postgresql_xa_resource_t *)context;
386  if (NULL == this->connection) {
387  LIXA_TRACE(("xta_postgresql_xa_rollback: PostgreSQL connection is "
388  "NULL!\n"));
389  THROW(OBJ_CORRUPTED);
390  }
391  /* call legacy switch file based code */
392  ret_cod = lixa_pq_rollback_core(&this->lssr, xid, rmid, flags);
393 
394  THROW(NONE);
395  } CATCH {
396  switch (excp) {
397  case OBJ_CORRUPTED:
398  break;
399  case NONE:
400  break;
401  default:
402  ret_cod = XAER_RMERR;
403  } /* switch (excp) */
404  } /* TRY-CATCH */
405  LIXA_TRACE(("xta_postgresql_xa_rollback/excp=%d/"
406  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
407  return ret_cod;
408 }
409 
410 
411 
413  int rmid, long flags)
414 {
415  enum Exception { OBJ_CORRUPTED
416  , NONE } excp;
417  int ret_cod = XAER_RMERR;
418 
419  LIXA_TRACE(("xta_postgresql_xa_prepare\n"));
420  TRY {
422  (xta_postgresql_xa_resource_t *)context;
423  if (NULL == this->connection) {
424  LIXA_TRACE(("xta_postgresql_xa_prepare: PostgreSQL connection is "
425  "NULL!\n"));
426  THROW(OBJ_CORRUPTED);
427  }
428  /* call legacy switch file based code */
429  ret_cod = lixa_pq_prepare_core(&this->lssr, xid, rmid, flags);
430 
431  THROW(NONE);
432  } CATCH {
433  switch (excp) {
434  case OBJ_CORRUPTED:
435  break;
436  case NONE:
437  break;
438  default:
439  ret_cod = XAER_RMERR;
440  } /* switch (excp) */
441  } /* TRY-CATCH */
442  LIXA_TRACE(("xta_postgresql_xa_prepare/excp=%d/"
443  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
444  return ret_cod;
445 }
446 
447 
448 
450  int rmid, long flags)
451 {
452  enum Exception { OBJ_CORRUPTED
453  , NONE } excp;
454  int ret_cod = XAER_RMERR;
455 
456  LIXA_TRACE(("xta_postgresql_xa_commit\n"));
457  TRY {
459  (xta_postgresql_xa_resource_t *)context;
460  if (NULL == this->connection) {
461  LIXA_TRACE(("xta_postgresql_xa_commit: PostgreSQL connection is "
462  "NULL!\n"));
463  THROW(OBJ_CORRUPTED);
464  }
465  /* call legacy switch file based code */
466  ret_cod = lixa_pq_commit_core(&this->lssr, xid, rmid, flags);
467 
468  THROW(NONE);
469  } CATCH {
470  switch (excp) {
471  case OBJ_CORRUPTED:
472  break;
473  case NONE:
474  break;
475  default:
476  ret_cod = XAER_RMERR;
477  } /* switch (excp) */
478  } /* TRY-CATCH */
479  LIXA_TRACE(("xta_postgresql_xa_commit/excp=%d/"
480  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
481  return ret_cod;
482 }
483 
484 
485 
487  XID *xids, long count, int rmid, long flags)
488 {
489  enum Exception { OBJ_CORRUPTED
490  , NONE } excp;
491  int ret_cod = XAER_RMERR;
492 
493  LIXA_TRACE(("xta_postgresql_xa_recover\n"));
494  TRY {
496  (xta_postgresql_xa_resource_t *)context;
497  if (NULL == this->connection) {
498  LIXA_TRACE(("xta_postgresql_xa_recover: PostgreSQL connection is "
499  "NULL!\n"));
500  THROW(OBJ_CORRUPTED);
501  }
502  /* call legacy switch file based code */
503  ret_cod = lixa_pq_recover_core(&this->lssr, xids, count, rmid, flags);
504 
505  THROW(NONE);
506  } CATCH {
507  switch (excp) {
508  case OBJ_CORRUPTED:
509  break;
510  case NONE:
511  break;
512  default:
513  ret_cod = XAER_RMERR;
514  } /* switch (excp) */
515  } /* TRY-CATCH */
516  LIXA_TRACE(("xta_postgresql_xa_recover/excp=%d/"
517  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
518  return ret_cod;
519 }
520 
521 
522 
524  int rmid, long flags)
525 {
526  enum Exception { OBJ_CORRUPTED
527  , NONE } excp;
528  int ret_cod = XAER_RMERR;
529 
530  LIXA_TRACE(("xta_postgresql_xa_forget\n"));
531  TRY {
533  (xta_postgresql_xa_resource_t *)context;
534  if (NULL == this->connection) {
535  LIXA_TRACE(("xta_postgresql_xa_forget: PostgreSQL connection is "
536  "NULL!\n"));
537  THROW(OBJ_CORRUPTED);
538  }
539  /* call legacy switch file based code */
540  ret_cod = lixa_pq_forget_core(&this->lssr, xid, rmid, flags);
541 
542  THROW(NONE);
543  } CATCH {
544  switch (excp) {
545  case OBJ_CORRUPTED:
546  break;
547  case NONE:
548  break;
549  default:
550  ret_cod = XAER_RMERR;
551  } /* switch (excp) */
552  } /* TRY-CATCH */
553  LIXA_TRACE(("xta_postgresql_xa_forget/excp=%d/"
554  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
555  return ret_cod;
556 }
#define XAER_RMERR
Definition: xa.h:332
void xta_postgresql_xa_resource_delete(xta_postgresql_xa_resource_t *xa_resource)
xta_postgresql_xa_resource_t * xta_postgresql_xa_resource_new(PGconn *connection, const char *name, const char *open_info)
#define LIXA_RC_INTERNAL_ERROR
Definition: lixa_errors.h:122
#define LIXA_RC_OK
Definition: lixa_errors.h:115
int xta_postgresql_xa_close(xta_xa_resource_t *context, char *xa_info, int rmid, long flags)
void xta_acquired_xa_resource_clean(xta_acquired_xa_resource_t *xa_resource)
int xta_postgresql_xa_end(xta_xa_resource_t *context, const XID *xid, int rmid, long flags)
void xta_postgresql_xa_resource_clean(xta_postgresql_xa_resource_t *xa_resource)
int xta_postgresql_xa_prepare(xta_xa_resource_t *context, const XID *xid, int rmid, long flags)
int xta_postgresql_xa_resource_init(xta_postgresql_xa_resource_t *xa_resource, PGconn *connection, const char *name, const char *open_info)
int xta_postgresql_xa_recover(xta_xa_resource_t *context, XID *xids, long count, int rmid, long flags)
#define LIXA_RC_G_TRY_MALLOC_ERROR
Definition: lixa_errors.h:563
void lixa_iface_set_xta(lixa_iface_t *iface, const struct xta_iface_s *xta, xta_xa_resource_t *context)
int xta_postgresql_xa_commit(xta_xa_resource_t *context, const XID *xid, int rmid, long flags)
Definition: xa.h:28
int xta_postgresql_xa_forget(xta_xa_resource_t *context, const XID *xid, int rmid, long flags)
long flags
Definition: xta_iface.h:42
struct act_rsrmgr_config_s act_rsrmgr_config
int xta_acquired_xa_resource_init(xta_acquired_xa_resource_t *xa_resource, const struct xta_iface_s *iface, const char *name, const char *open_info)
int xta_postgresql_xa_start(xta_xa_resource_t *context, const XID *xid, int rmid, long flags)
static const struct xta_iface_s xta_postgresql_iface
char name[RMNAMESZ]
Definition: xta_iface.h:38
#define XA_OK
Definition: xa.h:324
int xta_postgresql_xa_rollback(xta_xa_resource_t *context, const XID *xid, int rmid, long flags)
#define TMNOFLAGS
Definition: xa.h:141
int xta_postgresql_xa_open(xta_xa_resource_t *context, char *xa_info, int rmid, long flags)

Copyright © 2009-2019, Christian Ferrari tiian@users.sourceforge.net http://www.tiian.org/