| Oracle Internet File System Developer Reference Release 9.0.1.1.0 Part Number A90093-02 |
|
Chapter 4, "Creating an Oracle Internet File System Document" enables developers to get started building custom applications by introducing how to establish a session with Oracle 9iFS and use the session to create objects. This chapter discusses session management in more detail by covering the following advanced topics:
When you build a custom application, such as an agent, a protocol server, or a Web client, the first thing it must do is establish a session with Oracle 9iFS. The session enables the application to authenticate the user, cache information that the user is working with, and perform operations in the repository, such as searching for information, browsing folders, creating, updating, and deleting objects.
Oracle 9iFS provides a highly scalable and performant framework for managing sessions. The framework enables applications to horizontally scale by distributing the application's processing load across multiple middle-tier machines. The framework also enables applications to optimize memory usage and performance by caching information and pooling database resources.
The framework employs two Java objects to manage sessions:
LibraryServices (service) are responsible for:
Each LibraryService is responsible for connecting to the Oracle9i database and pooling database resources. The service connects to the database via Net8, enabling connections to be made locally on the machine running the database or remotely from a middle-tier machine. Consequently, services enable you to scale your application by distributing its processing load and memory demands across multiple host machines. Distributing your application in this manner also allows you to manage the accessibility of your applications by placing them on host machines that are accessible to different types of users. For example, you can place a Web application on a host machine outside of the corporate firewall to make it accessible to Internet users. You can place an SMB-based application on a host machine that is accessible to corporate users via an internal network. If your company is distributed across several worldwide sites, you can distribute an Intranet application across host machines at each site.
Applications use services to obtain a session with Oracle 9iFS. To establish a session for the application, the service first authenticates the user. If the user credentials are valid, the service provides a session that the application can use to interact with Oracle 9iFS.
Each service manages a set of sessions. The service is responsible for timing out inactive sessions, tracing session activity, maintaining a cache of information accessed by the sessions, and publishing events incurred by operations performed by sessions.
Oracle 9iFS services also allow you to optimize the performance, network demand and memory usage of your application. Each service maintains a cache of information accessed by the sessions. By caching information, the service decreases the number of Java objects that would be constructed by sessions and the number of calls that would retrieve data from the database.
Oracle 9iFS services are also used to handle events. The services publish events that occur in Oracle 9iFS so that sessions can process the events.
|
NOTE: For instructions on handling events, see Chapter 13, "Creating Custom Servers". For instructions on configuring services, consult the Oracle 9iFS Setup and Administration Guide. |
LibrarySessions (sessions) are responsible for:
he Oracle 9iFS session maintains its own state. For example, the session keeps track of the current user and whether administration mode is enabled.
The session acts as a factory for all operations performed by the application. The application uses the session to create, modify, and delete objects. The application also uses the session to execute queries for information in Oracle 9iFS, and to get collections of frequently used objects, such as the DirectoryUser collection, ClassObject collection, and SystemAccessControlList collection.
The Oracle 9iFS session is also used to perform operations synchronously within transactions. The application uses the session to begin a transaction, during which all operations are reflected only within the session's cache. Subsequently, the application can use the session to abort the transaction, effectively rolling back all changes, or commit the transaction, effectively making the changes in the repository.
The session maintains a cache of information that is being processed by the session. The session's cache reflects the session-specific state of the information. For example, all operations performed during a transaction are visible in the session's cache, but are not visible to other sessions until the transaction is committed. When the session commits the changes to Oracle 9iFS, the service informs all other sessions of the changes so that they can properly update their caches.
Oracle 9iFS Java API provides two Java classes that your application can use manage sessions: LibraryService and LibrarySession. These classes are supported by a few other classes, located in the oracle.ifs.common package, that are used to authenticate session users.
Services are represented as instances of the LibraryService class. The LibraryService class provides methods that the application can use to connection to the Oracle database and establish a session with Oracle 9iFS.
Table 16-1 lists the most commonly used methods on LibraryService.
|
|
Establishes a connection to Oracle 9iFS. It receives an instance of ConnectionOption that holds the user's credentials and other connection information. |
Sessions are represented by instances of the LibrarySession class. The LibrarySession class provides methods that the application can use to perform operations in Oracle 9iFS, such as creating, modifying, and deleting Oracle 9iFS objects, and searching the repository.
Table 16-2 lists the most commonly used methods on LibrarySession.
When the LibraryService establishes a connection, it must be passed a set of connection options, including the Oracle 9iFS server being connected to, and the user's credentials. The ConnectOptions class in the oracle.ifs.common package holds the connection information that is passed to the LibraryService, including the Locale, Service name, and Service password. In addition, the class CleartextCredential is used by an application to pass the user credentials to the LibraryService, which uses the credentials to authenticate the user.
Table 16-3 lists the most commonly used methods on ConnectOptions.
Table 16-4 lists the most commonly used methods on ClearTextCredential.
|
setName() |
Determines the name of the user. |
|
setPassword() |
Determines the user's password. The password is passed unencrypted. |
When applications use the Session Management Classes to establish a session with Oracle 9iFS, they inherently benefit from the caching, database resource pooling, and session management capabilities of the Oracle 9iFS service. In addition, the application can explicitly call the session management classes to perform fundamental tasks in Oracle 9iFS.
Table 16-5 lists the various tasks that an application can perform with the session management classes. It also references the section or chapter that describes how to perform these tasks in detail.
| Task | Chapter / Section |
|---|---|
|
Connecting to the Database and Authenticating Users |
Chapter 4, "Creating an Oracle Internet File System Document" describes how the session management classes can be used to connect to the database, authenticate the user's credentials, and establish a session with Oracle 9iFS. |
|
Creating Objects |
Chapter 4, "Creating an Oracle Internet File System Document" discusses how to use the LibrarySession and Definition classes to create documents. |
|
Querying the Repository |
Chapter 8, "Building Search Applications" discusses how the LibrarySession class can be used to construct Selectors and Searches which are used to query the repository. |
|
Managing Transactions |
The "Managing Transactions" section discusses how the application can call methods on the session to perform operations synchronously in transactions. |
|
Handling Events |
Chapter 13, "Creating Custom Servers"discusses how applications can use the session management classes to post and process events. |
|
Tracing |
The Oracle 9iFS Developer's Reference does not currently cover this topic. |
When an application performs an operation in Oracle 9iFS, such as creating, modifying, or deleting an object, the changes are immediately committed to the database. However, in some cases the application may need to commit a set of dependent operations together. That is, the application needs to ensure that if all operations cannot be successfully performed, none of the operations are performed. That is, the operations must be atomically committed or rolled back. The Oracle 9iFS session management framework enables applications to wrap operations in a transaction block that can be used to control when the changes are committed to the database.
The LibrarySession class in the Oracle 9iFS Java API provides methods that are used to manage transactions: beginTransaction(), abortTransaction(), and completeTransaction(). These methods enable applications to define the boundaries of a transaction block, test the feasibility of the operations in the transaction, and commit or rollback the changes made by the operations based on their success or failure.
Transactions are managed in the following steps:
Example 16-1 illustrates how an application can use the LibrarySession to manage a transaction.
/* * The following example illustrates how to delete a group * and all of its members in a single transaction. * If a member cannot be removed, the group will not be deleted. */
DirectoryGroup dg = .... DirectoryObject[] members = dg.getAllMembers(); DirectoryObject m;
Transaction transaction = session.beginTransaction();
try {
int count = (members == null) ? 0 : members.length; for (int i = 0; i < size; i++) { m = members[i]; dg.removeMember(m); }
session.completeTransaction(transaction); } catch (IfsException e) {
session.abortTransaction(transaction); throw e; }
Oracle 9iFS operations in the transaction that perform Data Definition Language (DDL) operations in the database (e.g., creating tables, views, and columns) will still be immediately committed to the database. Calling the abortTransaction() method will not rollback the database changes. The following Java API methods perform DDL operations in the database:
The following are the rules for how changed LibraryObjects become available to sessions:
1000 * (PollForEventsFromOtherServicesPeriod +
TransportEventsToOtherServicesPeriod) +
EventPollerPeriod milliseconds
Some custom application may need to use Oracle 9iFS transaction management capabilities not only to manage operations performed in Oracle 9iFS, but to manage operations performed in an external database schema. For example, you might build an application that integrates Oracle 9iFS with another database schema that stores product data. Whenever a design document is updated in Oracle 9iFS, the custom application must also update the status of the corresponding product in the other database schema. If the product cannot be updated successfully, then the changes to the design document must be rolled back.
Oracle 9iFS Java API provides a set of server-side Java classes, included in the oracle.ifs.server.sql package, that allow custom applications to implement overrides that perform operations against Oracle 9iFS objects and external databases in a single transaction. The classes allow an application to connect to an external database schema, construct a SQL statement, and execute the statement. The classes encapsulate the JDBC calls that perform these tasks against the external database so that Oracle 9iFS maintains control of how the operations are committed and rolled back.
The following are the Java classes package, that are used to perform JDBC operations on the server:
The IfsConnection class is used to establish a JDBC connection to the external database. It encapsulates java.sql.Connection.
IfsStatement class is used to construct and execute static SQL statements. It encapsulates java.sql.Statement.
The IfsPreparedStatement is used to construct and execute pre-compiled SQL statements containing parameter variables. It encapsulates java.sql.PreparedStatement.
IfsCallableStatement class is used to construct and execute SQL stored procedures. It encapsulates java.sql.CallableStatement.
To represent a session with a database schema, the custom application would instantiate the IfsConnection class. IfsConnection is instantiated by calling the getIfsConnection() method on the server-side representation of the application's session (e.g., S_LibrarySession).
Use the S_LibrarySession to construct an IfsConnection object.
IfsConnection conn = m_Session.getIfsConnection();
The IfsConnection instance is subsequently used to construct the Java objects used to represent SQL statement: IfsStatement, IfsPreparedStatement, and IfsCallableStatement. Example 16-3, Example 16-4, and Example 16-5 illustrate how the IfsConnection is used to construct these objects.
Once an IfsConnection object is constructed, it can be used obtain an IfsStatement. IfsStatement is used to construct SQL statements that are static, that is statements that don't use bind variables. To uses an IfsStatement, follow these steps:
protected void extendedPreInsert(OperationState opState, S_LibraryObjectDefinition def) throws IfsException { super.extendedPreInsert(opState, def);
IfsConnection conn = m_Session.getIfsConnection(); try {
IfsStatement stmt = conn.createStatement();
AttributeValue av = def.getAttribute("NAME"); String recipe = av.getString(m_Session); S_DirectoryUser user = m_Session.getUser(); String name = user.getName(); String sqlstmt = "insert into your_custom_table (chef, recipe) " + "values ('wtalman','Curry Lamb')";
stmt.execute(sqlstmt); }
catch (SQLException e) { throw new IfsException(98765, e); }
finally { if (stmt != null) { stmt.dispose(); } } }
As an alternative to IfsStatements, IfsPreparedStatements can be used to call pre-compiled SQL statement. To use an IfsPreparedStatement, follow these steps:
setString() method on the IfsPreparedStatement object.
() method on the IfsPreparedStatement method to execute the SQL statement.
protected void extendedPreUpdate(OperationState opState, S_LibraryObjectDefinition def) throws IfsException { super.extendedPreUpdate(opState, def);
IfsConnection conn = m_Session.getIfsConnection(); try {
AttributeValue av = def.getAttribute("NAME"); String recipe = av.getString(m_Session); S_DirectoryUser user = m_Session.getUser(); String name = user.getName(); String sqlstmt = "insert into your_custom_table (chef, recipe) " + "values (?, ?)";
IfsPreparedStatement pstmt = conn.prepareStatement(sqlstmt);
pstmt.setString(1, name); pstmt.setString(2, recipe);
pstmt.execute(sqlstmt); }
catch (SQLException e) { throw new IfsException(98765, e); }
finally { if (pstmt != null) { pstmt.dispose(); } } }
The IfsConnection can also be used to construct an IfsCallableStatement to execute a SQL stored procedure. To use an IfsCallableStatement, follow these steps:
protected void extendedPreFree(OperationState opState, S_LibraryObjectDefinition def) throws IfsException { super.extendedPreFree(opState, def);
IfsConnection conn = m_Session.getIfsConnection(); try {
AttributeValue av = def.getAttribute("NAME"); String recipe = av.getString(m_Session); String sqlstmt = "{call yourcustompackage.test(?)}";
IfsCallableStatement cstmt = conn.prepareCall(sqlstmt);
cstmt.setString(1, recipe);
cstmt.execute(); }
catch (SQLException e) { // should write your own exception message throw new IfsException(98765, e); }
finally { if (cstmt != null) { cstmt.dispose(); } } }
Oracle 9iFS places the following restrictions on performing JDBC operations within Oracle 9iFS transactions:
All permissible and valid operations on Oracle 9iFS tables are exposed through the Oracle 9iFS Java API. Oracle 9iFS does not support performing JDBC operations against tables in the Oracle 9iFS schema. Operations that directly manipulate schema objects (e.g., tables and views) or data stored in the schema (e.g., document data) will cause the caches maintained by Oracle 9iFS services to become stale and can corrupt Oracle 9iFS objects.
You cannot use Oracle 9iFS to manage transactions across JDBC connections that invoke DDL statements, such as create or drop table. DDL statements are implicitly committed by the Oracle database and could interfere with Oracle 9iFS transaction management.
The following methods on IfsConnection are associated with JDBC transactions and will immediately throw an SQLException to restrict the use of such operations during the Oracle 9iFS connection:
The connections used in the read/write connection pool cache must not be committed or rolled back explicitly. Otherwise, the Oracle 9iFS transaction management cannot effectively rollback in response to an IfsException.
Oracle 9iFS is installed with sample code that helps you get started working with the Java API. The API sample code is located in the <ORACLE_HOME>/9ifs/samplecode/api directory.
Table 16-6 lists the API sample code that is relevant to this chapter.
| Class | Usage |
|---|---|
|
BaseSample.java |
Starts a service, establishes and disconnects a session. |
|
CreateLargeGroups.java |
Demonstrates the use of transactions. |
|
|
![]() Copyright © 2001 Oracle Corporation. All Rights Reserved. |
|