使用多线程并发写数据库,如果检测到已经写入该数据,则记标记位。
我的程序是
(void) OCIInitialize((ub4) OCI_OBJECT,
(dvoid *)0, (dvoid * (*)()) 0,
(dvoid * (*)()) 0, (void (*)()) 0 ); (void)OCIHandleAlloc( (dvoid *) NULL, (dvoid **) &envhp, (ub4) OCI_HTYPE_ENV,
0, (dvoid **) &tmp);
// initialize the environmental handle
(void) OCIEnvInit( &envhp, (ub4) OCI_DEFAULT, 21, (dvoid **) &tmp );
// get the error handle
(void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &errhp,
(ub4) OCI_HTYPE_ERROR,
52, (dvoid **) &tmp);
// server contexts
(void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &srvhp,
(ub4) OCI_HTYPE_SERVER,
52, (dvoid **) &tmp);
// attach the server
(void) OCIServerAttach( srvhp, errhp, (text *) servername, (sb4)strlen(servername),
(ub4) OCI_DEFAULT);
// get the service handle
(void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &svchp,
(ub4) OCI_HTYPE_SVCCTX,
52, (dvoid **) &tmp); // get the statement handle
(void)OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &stmthp,
(ub4) OCI_HTYPE_STMT, 0, 0);
// set attribute server context in the service context
(void) OCIAttrSet( (dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX,
(dvoid *) srvhp, (ub4) 0,
(ub4) OCI_ATTR_SERVER, (OCIError *) errhp);
// get the user handle
(void) OCIHandleAlloc((dvoid *)envhp, (dvoid **)&authp,
(ub4)OCI_HTYPE_SESSION, 0, (dvoid **)0);
// set the attribute user name
(void) OCIAttrSet((dvoid *) authp, (ub4)OCI_HTYPE_SESSION,
(dvoid *)username, (ub4)strlen(username),
(ub4)OCI_ATTR_USERNAME, errhp);
// set the attribute password
(void) OCIAttrSet((dvoid *) authp, (ub4)OCI_HTYPE_SESSION,
(dvoid *)password, (ub4)strlen(password),
(ub4)OCI_ATTR_PASSWORD, errhp); // authenticate if(status =OCISessionBegin(svchp,errhp,authp,OCI_CRED_RDBMS,(ub4)OCI_DEFAULT))
{
checkerr(errhp,status);
printf("error user/password/databasename\n");
cleanup();
exit(0);
}
// set the attribute user context of the service handle
(void) OCIAttrSet((dvoid *)svchp, (ub4)OCI_HTYPE_SVCCTX,
(dvoid *)authp, (ub4)0,
(ub4)OCI_ATTR_SESSION, errhp); insert my date to datebase
可是实际使用的时候,有的时候并发会将相同数据写入多次(就好像是两者几乎同时写入数据库一样),标记位没有起到作用。
可是数据库在处理client端的事务的时候,应该是有先后顺序的啊
请问我上面的那段代码有问题吗?
谢谢!
我的程序是
(void) OCIInitialize((ub4) OCI_OBJECT,
(dvoid *)0, (dvoid * (*)()) 0,
(dvoid * (*)()) 0, (void (*)()) 0 ); (void)OCIHandleAlloc( (dvoid *) NULL, (dvoid **) &envhp, (ub4) OCI_HTYPE_ENV,
0, (dvoid **) &tmp);
// initialize the environmental handle
(void) OCIEnvInit( &envhp, (ub4) OCI_DEFAULT, 21, (dvoid **) &tmp );
// get the error handle
(void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &errhp,
(ub4) OCI_HTYPE_ERROR,
52, (dvoid **) &tmp);
// server contexts
(void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &srvhp,
(ub4) OCI_HTYPE_SERVER,
52, (dvoid **) &tmp);
// attach the server
(void) OCIServerAttach( srvhp, errhp, (text *) servername, (sb4)strlen(servername),
(ub4) OCI_DEFAULT);
// get the service handle
(void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &svchp,
(ub4) OCI_HTYPE_SVCCTX,
52, (dvoid **) &tmp); // get the statement handle
(void)OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &stmthp,
(ub4) OCI_HTYPE_STMT, 0, 0);
// set attribute server context in the service context
(void) OCIAttrSet( (dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX,
(dvoid *) srvhp, (ub4) 0,
(ub4) OCI_ATTR_SERVER, (OCIError *) errhp);
// get the user handle
(void) OCIHandleAlloc((dvoid *)envhp, (dvoid **)&authp,
(ub4)OCI_HTYPE_SESSION, 0, (dvoid **)0);
// set the attribute user name
(void) OCIAttrSet((dvoid *) authp, (ub4)OCI_HTYPE_SESSION,
(dvoid *)username, (ub4)strlen(username),
(ub4)OCI_ATTR_USERNAME, errhp);
// set the attribute password
(void) OCIAttrSet((dvoid *) authp, (ub4)OCI_HTYPE_SESSION,
(dvoid *)password, (ub4)strlen(password),
(ub4)OCI_ATTR_PASSWORD, errhp); // authenticate if(status =OCISessionBegin(svchp,errhp,authp,OCI_CRED_RDBMS,(ub4)OCI_DEFAULT))
{
checkerr(errhp,status);
printf("error user/password/databasename\n");
cleanup();
exit(0);
}
// set the attribute user context of the service handle
(void) OCIAttrSet((dvoid *)svchp, (ub4)OCI_HTYPE_SVCCTX,
(dvoid *)authp, (ub4)0,
(ub4)OCI_ATTR_SESSION, errhp); insert my date to datebase
可是实际使用的时候,有的时候并发会将相同数据写入多次(就好像是两者几乎同时写入数据库一样),标记位没有起到作用。
可是数据库在处理client端的事务的时候,应该是有先后顺序的啊
请问我上面的那段代码有问题吗?
谢谢!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货