Skip Headers

# Acquiring a Client Session That Uses Exclusive Connections

Previous

Next

Example: Acquiring a Client Session that Uses Connection Properties illustrates how to configure a `ConnectionPolicy` and use it to acquire a client session that uses exclusive connections.

Acquiring a Client Session that Uses Connection Properties

    ConnectionPolicy connectionPolicy = new ConnectionPolicy();
    // Use an exclusive connection for the session
    connectionPolicy.setShouldUseExclusiveConnection(true);

    Session clientSession = server.acquireClientSession(connectionPolicy);
    // By default, an exclusive connection will be acquired lazily

An exclusive connection is allocated from a shared connection pool. The connection is dedicated to the client session that acquires it.

Note:

Typically, the life cycle of a client session is the duration of a server request. However, if you are using JTA, it is the life cycle of a JTA transaction.

You cannot hold the client session across the JTA transaction boundaries. If you are not using a unit of work in your transaction and you are configuring a client session to use an exclusive connection (see Configuring Exclusive Isolated Client Sessions for Virtual Private Database), you must explicitly acquire and release the session when you are finished using it. Although client sessions have a finalizer that would release the session when it is garbage-collected, you must not rely on the finalizer and release the exclusive client session (or a non-lazy session) in the application to release the data source connection. Note that the requirement to release the session in not JTA-specific.

If you are using a unit of work (see Using Advanced Unit of Work API), you do not have to worry about releasing its client session as the unit of work always automatically releases it at the end of the JTA transaction.

A named query can also use an exclusive connection (see "Configuring Named Query Advanced Options").

For more information, see the following:

- "Acquiring a Client Session That Does Not Use Lazy Connection Allocation"

- "Configuring Connection Policy".