Oracle8的OCI编程步骤
The following section describes the stepsthat are required of a release 8.0 OCI application:
·Initialization,connection, and Session Creation初始化、连接和创建会话
·Processing SQLStatements 处理SQL语句
·Commit or Rollback 提交事务或者回滚事务
·Terminating the Application终止应用程序
·Error Handling 错误处理
1. Initialization, connection, andSession Creation初始化、连接和创建会话
This section describes how to initialize theOracle 8 OCI environment, establish a connection to a server, and authorize auser to perform actions against a database.
The three main steps in initializing the OCIenvironment are described in this section:
1.1 Initialize the OCI Process 初始化OCI进程
Initialize the OCI Process 初始化OCI进程
Allocate Handles and Descriptors 分配句柄和描述符
Initialize the Application, Connection, and Authorization 初始化应用程序、连接和认证Allocate Handles and Descriptors 分配句柄和描述符
The initialize process call, OCIInitialize(),must be invoked before any other OCI call. The mode parameter of this call specifieswhether the application will run in a threaded environment (mode =OCI_THREADED), and whether or not it will use objects (mode = OCI_OBJECT).Initializing in object mode is necessary if the application will be binding anddefining objects, or if the application will be using the OCI’s objectnavigation calls.
The program may also choose to use neither of thesefeatures (mode = OCI_DEFAULT) or both, separating the options with avertical bar (mode = (OCI_THREADED | OCI_OBJECT)).
The OCIInitialize() call can also specifyuser-defined memory management functions.
Allocate Handles and Descriptors 分配句柄和描述符
Oracle provides OCI functions to allocate and deallocatehandles and descriptors. You must allocate handles using OCIHandleAlloc() beforepassing them into an OCI call, unless the OCI call allocates the handles for you(e.g. OCIBindByPos()).
Initialize theApplication, Connection, and Authorization 初始化应用程序、连接和认证
Anapplication must call OCIEnvCreate() to initialize the OCI environment handle.
Followingthis step, the application has two options for establishing a server connectionand beginning a user session: Single User, Single Connection; or MultipleSessions or Connections.
1. Option1:Single User, Single Connection
2. Option2:Multiple Sessions or Connections
Thisoption uses explicit attach and begin session calls.
Ifan application needs to maintain multiple user sessions on a databaseconnection, the application requires a different set of calls to set up thesessions and connections. This includes specific calls to attach to the serverand begin sessions:
·OCIServerAttach() creates an access path to the data server for OCIoperations.
·OCISessionBegin() establishes a session for a user against a particularserver. This call is required for the user to be able to execute any operationon the server.
Thesecalls set up an operational environment that allows you to execute SQL andPL/SQL statements against a database. The database must be up and runningbefore the calls are made, or else they will fail.
Terminating theApplication
AnOCI application should perform the following three steps before it terminates:
1. Deletethe user session by calling OCISessionEnd() for each session.
2. Deleteaccess to the data source(s) by calling OCIServerDetach() for each source.
3. Explicitlydeallocate all handles by calling OCIHandleFree() for each handle.
4. Deletethe environment handle, which deallocates all other handles associated with it.
Note: When a parent OCI handle is freed, anychild handles associated with it are freed automatically.
Thecalls to OCIServerDetach() and OCISessionEnd() are not mandatory, but are recommended.If the application terminates, and OCITransCommit() (transaction commit) hasnot been called, any preceding transactions are automatically rolled back.