Connecting to a Service: An Example
You can use this example to see how to create a service, and see several examples of connecting to that service using different client methods.
The service is enabled for run-time load balancing using the following scenario:
- Service Name:
HR.example.com Running on database named CRM
- The system consists of 4 nodes
SERVICE_TIMEis specified as the value for the-rlbgoalparameter- The SCAN address of the listener is
rws3010104-scan.example.com - The Listener port is 1585
The service has a cardinality of two, but if needed, can be offered by any of the CRM database instances. The service configuration is as follows:
- Preferred Instances: CRM1, CRM2
- Available Instances: CRM3, CRM4
LONGis specified as the value for the-clbgoalparameter
The application using this service takes advantage of Application Continuity, so you must set -failovertype and -commit_outcome. Use the default retention parameters, but set a 10 second delay between connection attempts, and set a limit of up to 40 retries before failing to get a connection.
Example: Creating the HR Service Using SRVCTL
In this example, you create the HR service using SRVCTL:
$ srvctl add service -db CRM -service HR.example.com -preferred CRM1,CRM2
-available CRM3,CRM4 -clbgoal LONG -failovertype TRANSACTION
-commit_outcome TRUE -failoverdelay 10 -failoverretry 40
Next, start the HR.example.com service:
$ srvctl start service -db CRM -service HR.example.com
The service is now available on up to two instances, and CRM1 and CRM2 are the preferred instances.
Example: Connecting to the HR Service from a JDBC Application
In this example, the application that connects to the HR service is a Java Database Connectivity (JDBC) application that is using the JDBC Universal Connection Pool with the JDBC thin driver.
A URL is constructed specifying the thin-style service name format for the database specifier. Fast Connection Failover is enabled, and remote Oracle Notification Service is configured, where the Oracle Notification Service daemon on the cluster listens on port 6200.
//import packages and register the driver
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.ucp.jdbc.PoolDataSourceFactory;
import oracle.ucp.jdbc.PoolDataSource;
PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();
//set the connection properties on the data source.
pds.setConnectionPoolName("FCFPool");
pds.setFastConnectionFailoverEnabled(true);
pds.setONSConfiguration("nodes=rws3010104-scan.example.com:6200");
pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
pds.setURL("jdbc:oracle:thin:@//rws3010104-scan.example.com:1585/HR.example.com");
pds.setUser("HR");
pds.setPassword("hr");
//Override any pool properties.
pds.setInitialPoolSize(5);
//Get a database connection from the datasource.
Connection conn = pds.getConnection();
// do some work
//return connection to pool
conn.close();
conn=null
Related Topics