📄 twophase.c
字号:
}
/* Attempt a connection to the remote SQL servers */
if (ct_connect(*conn1, SERVER1, CS_NULLTERM) != CS_SUCCEED)
{
return (CS_FAIL);
}
if (ct_connect(*conn2, SERVER2, CS_NULLTERM) != CS_SUCCEED)
{
return (CS_FAIL);
}
if (open_commit(*commit, SERVER2, CS_NULLTERM) != CS_SUCCEED)
{
return (CS_FAIL);
}
return (CS_SUCCEED);
}
/*
** INIT_CONN -
** Initialize the connection to the Server
**
** Parameters:
** cp The application context.
** username The user name to initialize
** password The password.
**
** Returns:
** The initialized connection on success, NULL on failure.
**
*/
CS_CONNECTION *init_conn(cp, username, password)
CS_CONTEXT *cp;
CS_CHAR *username;
CS_CHAR *password;
{
CS_CONNECTION *conn; /* Connection control struct */
/* Initialization */
conn = (CS_CONNECTION *)NULL; /* not allocated yet */
/* Allocate a connection handle for the connection attempt */
if (ct_con_alloc(cp, &conn) != CS_SUCCEED)
{
/*
** Send a message that we could not build a remote
** connection because of a memory allocation failure
*/
return ((CS_CONNECTION *)NULL);
}
/*
** Set up the login information for the remote DBMS.
** Also save control structures for access from other routines.
*/
if (ct_con_props(conn, CS_SET, CS_USERNAME, USERNAME,
CS_NULLTERM, (CS_INT *)NULL) != CS_SUCCEED)
{
(CS_VOID)ct_con_drop(conn);
return ((CS_CONNECTION *)NULL);
}
if (ct_con_props(conn, CS_SET, CS_PASSWORD, PASSWORD,
CS_NULLTERM, (CS_INT *)NULL) != CS_SUCCEED)
{
(CS_VOID)ct_con_drop(conn);
return ((CS_CONNECTION *)NULL);
}
if (ct_con_props(conn, CS_SET, CS_APPNAME, "Twophase",
CS_NULLTERM, (CS_INT *)NULL) != CS_SUCCEED)
{
(CS_VOID)ct_con_drop(conn);
return ((CS_CONNECTION *)NULL);
}
return (conn);
}
/*
** CLOSE_CONN - Close a connection
**
** Parameters:
** conn - the connection to close
**
** Returns:
** none.
*/
CS_RETCODE close_conn(conn)
CS_CONNECTION *conn;
{
/* If connection with server active, close it now */
if (conn != (CS_CONNECTION *)NULL)
{
if (ct_close(conn, CS_UNUSED) != CS_SUCCEED)
{
(CS_VOID)ct_close(conn, CS_FORCE_CLOSE);
return (CS_FAIL);
}
if (ct_con_drop(conn) != CS_SUCCEED)
return (CS_FAIL);
}
return (CS_SUCCEED);
}
/*
** server_err_handler - Server errors
** Open Client calls this routine when it receives messages from the
** server. It reads the message information and prints it to the
** console.
**
** Parameters:
** cp A pointer to the context structure for this
** application.
**
** chp A pointer to the connection handle on which the
** error occurred. (It can be NULL.)
**
** msgp A pointer to a structure containing information
** about the server message.
** Returns:
** CS_SUCCEED
*/
CS_RETCODE server_err_handler(cp, chp, msgp)
CS_CONTEXT *cp;
CS_CONNECTION *chp;
CS_SERVERMSG *msgp;
{
/*
** If chp is (CS_CONNECTION *)NULL, this error occurred when no
** connection handle was active. Just return.
*/
if (chp == (CS_CONNECTION *)NULL)
return (CS_SUCCEED);
if (msgp->msgnumber == 5701 || msgp->msgnumber == 5703 ||
msgp->msgnumber == 5704)
{
return (CS_SUCCEED);
}
/* Build and print the client message. */
(CS_VOID)fprintf(stdout,
"Server: msgno(%d) severity(%d) state(%d)\n",
msgp->msgnumber, msgp->severity, msgp->state);
/* Null terminate message. */
switch((int)msgp->textlen)
{
case CS_NULLTERM:
/* Message already null terminated. */
break;
case CS_UNUSED:
case 0:
/* No message specified. */
msgp->text[0] = '\0';
break;
default:
if (msgp->textlen < (CS_MAX_MSG - 1))
msgp->text[msgp->textlen] = '\0';
else
msgp->text[CS_MAX_MSG - 1] = '\0';
break;
}
/* Print the null terminated message. */
(CS_VOID)fprintf(stdout, "\t%s\n", msgp->text);
return(CS_SUCCEED);
}
/*
** client_err_handler - Open Client errors.
** This is the Open Client error handler. All Open Client connections
** use this error handler. The error messages are printed to the
** console.
**
** Parameters:
** chp A pointer to the connection handle on which this
** error occurred. (It can be NULL.)
**
** cmdp A pointer to the command handle active when error
** occurred. (It can be NULL.)
**
** emsgp A pointer to an Open Client CS_CLIENTMSG structure.
**
** Returns:
** CS_SUCCEED
*/
CS_RETCODE client_err_handler(chp, cmdp, emsgp)
CS_CONNECTION *chp;
CS_COMMAND *cmdp;
CS_CLIENTMSG *emsgp;
{
/*
** If chp is (CS_CONNECTION *)NULL, this error occurred when no
** connection handle was active. Just return cancel.
*/
if (chp == (CS_CONNECTION *)NULL)
return (CS_SUCCEED);
/*
** Format and print the message. */
(CS_VOID)fprintf(stdout,
"CT-Library error(L%ld O%ld S%ld N%ld) serverity(%d)\n",
CS_LAYER(emsgp->msgnumber),
CS_ORIGIN(emsgp->msgnumber),
CS_SEVERITY(emsgp->msgnumber),
CS_NUMBER(emsgp->msgnumber),
emsgp->severity);
(CS_VOID)fprintf(stdout, "\t%s\n", emsgp->msgstring);
/*
** If there was an operating system error, send the error
** message text for the OS error.
*/
if ((emsgp->osstringlen != 0) && (emsgp->osstringlen != CS_UNUSED))
{
(CS_VOID)fprintf(stdout,
"CT-Library OS error %ld - %s.\n",
emsgp->osnumber, emsgp->osstring);
}
return (CS_SUCCEED);
}
/*
** CS_ERR_HANDLER - CS-Library error handler.
** This routine is the CS-Library error handler used by this
** application. It is called by CS-Library whenever an error
** occurs. Here, we simply print the error and return.
**
** Parameters:
** cp A pointer to the connection handle for connection
** on which the message was received.
** (It can be NULL.)
**
** msg The structure containing information about the
** server message.
**
** Returns:
** CS_SUCCEED
*/
CS_RETCODE cs_err_handler(cp, msg)
CS_CONTEXT *cp;
CS_CLIENTMSG *msg;
{
/*
** Build the error message string to be logged.
*/
(CS_VOID)fprintf(stdout,
"CS-Library error(L%ld O%ld S%ld N%ld) serverity(%d)\n",
CS_LAYER(msg->msgnumber),
CS_ORIGIN(msg->msgnumber),
CS_SEVERITY(msg->msgnumber),
CS_NUMBER(msg->msgnumber),
msg->severity);
(CS_VOID)fprintf(stdout, "\t%s\n", msg->msgstring);
/*
** Print any operating system error information.
*/
if( msg->osstringlen > 0 )
{
(CS_VOID)fprintf(stdout,
"CS-Library OS error %ld - %s.\n",
msg->osnumber, msg->osstring);
}
/*
** All done.
*/
return (CS_SUCCEED);
}
/*
** EXEC_CONN
**
** This routine sends a command to the Server and ignores any resulting
** errors.
**
** Parameters:
** CS_CONNECTION *conn The server connection.
** CS_CHAR *query The query to execute.
**
** Returns:
** CS_SUCCEED Test command executed as expected.
** CS_FAIL Test command detected an unexpected result.
*/
CS_RETCODE exec_conn(conn, query)
CS_CONNECTION *conn;
CS_CHAR *query;
{
CS_INT ret;
CS_INT retcode;
CS_COMMAND *cmd;
/* Allocate a command buffer */
if (ct_cmd_alloc(conn, &cmd) != CS_SUCCEED)
{
return (CS_FAIL);
}
if (ct_command(cmd, CS_LANG_CMD, query, CS_NULLTERM,
CS_UNUSED) != CS_SUCCEED)
{
(CS_VOID)ct_cancel((CS_CONNECTION *)NULL, cmd, CS_CANCEL_ALL);
(CS_VOID)ct_cmd_drop(cmd);
return (CS_FAIL);
}
if (ct_send(cmd) != CS_SUCCEED)
{
(CS_VOID)ct_cancel((CS_CONNECTION *)NULL, cmd, CS_CANCEL_ALL);
(CS_VOID)ct_cmd_drop(cmd);
return (CS_FAIL);
}
while ((retcode = ct_results(cmd, &ret)) == CS_SUCCEED)
{
switch ((int)ret)
{
case CS_ROW_RESULT:
case CS_STATUS_RESULT:
while ((retcode = ct_fetch(cmd, CS_UNUSED, CS_UNUSED,
CS_UNUSED, NULL)) == CS_SUCCEED)
{
continue;
}
if (retcode != CS_END_DATA)
{
(CS_VOID)ct_cancel((CS_CONNECTION *)NULL,
cmd, CS_CANCEL_ALL);
(CS_VOID)ct_cmd_drop(cmd);
return (CS_FAIL);
}
case CS_CMD_FAIL:
(CS_VOID)ct_cancel((CS_CONNECTION *)NULL,
cmd, CS_CANCEL_ALL);
(CS_VOID)ct_cmd_drop(cmd);
return (CS_FAIL);
default:
if (ct_cancel((CS_CONNECTION *)NULL, cmd,
CS_CANCEL_CURRENT) != CS_SUCCEED)
{
(CS_VOID)ct_cancel((CS_CONNECTION *)NULL,
cmd, CS_CANCEL_ALL);
(CS_VOID)ct_cmd_drop(cmd);
return (CS_FAIL);
}
break;
}
}
if (retcode != CS_END_RESULTS)
{
(CS_VOID)ct_cancel((CS_CONNECTION *)NULL, cmd, CS_CANCEL_ALL);
(CS_VOID)ct_cmd_drop(cmd);
return (CS_FAIL);
}
if (ct_cmd_drop(cmd) != CS_SUCCEED)
{
return (CS_FAIL);
}
return (CS_SUCCEED);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -