DevPortal

How do I configure Datasource Cluster Resource for J2EE Applications?


Table of Contents


How do I configure Datasource Cluster Resource for J2EE Applications?

A Datasource Cluster (DSC) is a collection of datasources in a server. A DSC can be used for high-availability or for load balancing depending on its configuration. A DSC can be used in the same manner as datasources. When a client requests for a connection, DSC chooses one of its datasources and returns a connection.

Note: The datasources that are given to a DSC must point to databases present in a cluster internally, otherwise it might lead to data inconsistency. DSC does not try to replicate the actions performed on one datasource to another.

How do I use resource-config.xml to create DSC Resources?

You can configure a highly available DSC resource using resource-config.xml by adding a datasource-cluster tag under the datasource_clusters tag. The resource-config.xml looks as below:

<datasource-cluster name="ORCL_CLUSTER1" balance-load="true" main-child-node="ORCL_DS1">
  <datasource-node name="ORCL_DS1" weight="2"/>
  <datasource-node name="ORCL_DS2" weight="2"/>
</datasource-cluster-->

Table 1: Description of resource-config.xml parameters

ParametersDescription
nameThis is the JNDI name that would be used by the clients for looking up the datasource.
balance-loadSpecify whether the DSC should balance load or just ensure high-availability.
main-child-nodeThe first request that is made to the DSC is passed to this datasource. This value is mainly useful when balance-load is false. In case the DSC fails to find this node it randomly picks any other available datasource.

Every datasource that is a part of this DSC must be mentioned in a datasource-node element along with its weight attribute.

The weight attribute signifies the weightage of the node in comparison with the other nodes. The weight attribute in this element is optional and is used only if the balance-load="true". If the balance-load is set to true and no weight is provided, then it is given a default weightage of 1.

How do I use JDBC Resource MBean to create DSC Resources?

Once you have the Resource Service MBean, call the JDBCResourceMBean by entering JDBCResourceMBean jdbcResource= resourceServiceMBean.getJDBCResourceMBean();

Call the createClusteredDataSource method on this MBean:

public void createClusteredDataSource(String jndiName, boolean balanceLoad,
String[] dataSourceNames, int[] weights, String mainChildNode) throws ResourceException;

Table 2: Description of parameters taken in the JDBC Resource MBean

ParametersDescription
jndiNameRefers to the JNDI name of the clustered datasource.
balanceLoad trueImplies that the cluster should balance load.
balanceLoad falseImplies that the cluster is for high availability mode.
dataSourceNamesRefers to the JNDI names of the datasources that should be included in the Clustered datasource.
weightRefers to the weightage of each of the datasource.
mainChildNodeRefers to the JNDI name of the datasource that has to be set as the main child of the Clustered datasource.

Working of DSCs in HA-mode

Every connection request to DSC is obtained from the main child node or the first available datasource in the collection (if main child node is not available). If obtaining a connection from datasource fails, a connection is sought sequentially from the next datasource in the collection.

Working of DSCs in LB-mode in HA-mode

Connection requests for load balancing DSCs are served from any datasources in the list, starting with the main child node if available. Datasources are added in a random order and are accessed using a round-robin scheme.

Working of DSCs in Transaction

When in transaction, the same connection is returned to the client for all the requests within that transaction.

How do I modify DSCs?

DSCs can be modified using its MBean. To get the DSCs MBean, call getJDBCClusteredDataSource(String jndiname) on the jdbcResourceMbean where jndiName is the JNDI name of the required DSC.

Methods on MBean

/*** @return jndi name of datasource. */
	public String getJNDIName();

/*** @returns the objectNames of all the child Nodes */
	public ObjectName[] getChildDataSourcesObjectNames();

/***@returns the child Nodes */
	public JDBCSelfManagedDataSourceMBean[] getChildDataSources();

/*** method to remove a dataSource from the cluster
* @param objectName objectName of the dataSource to be removed from the cluster */
	public boolean removeFromCluster(ObjectName name);

/*** method to add a DataSource to the ClusteredDataSource
*@param objectName: objectName of the dataSource to be added to the cluster
*@throws ResourceException */
	public void addToCluster(ObjectName name)throws ResourceException;

/*** method to add a DataSource to the ClusteredDataSource
*@param objectName objectName of the dataSource to be added to the cluster
* @param weight weight of the DataSource
* @throws ResourceException */
    public void addToCluster(ObjectName name, int weight)throws ResourceException;

/*** @return whether ClusteredDataSource balances the load */
	public boolean isLoadBalancing();

/*** method to enable or disable load balancing
* @param enable: if enable is true then the cluster balances the load
* else it is in failover mode */
	public void setLoadBalancing(boolean enable);

/*** method to set the main child
* @param name of the child */
	public void setMainChildNode(ObjectName name)throws ResourceException;
        //Only in case of PureFailover

/*** @return the objectName of the main childNode */
	public ObjectName getMainChildNode();

/*** method to set the weights to some/all of the child nodes
* @param childNodes objectNames of the childNodes whose weight has to be set
* @param weights weights of the corresponding childNodes
* @throws ResourceException */
public void setNodeWeights(ObjectName[]childNodes, int[]weights)throws ResourceException;
//ObjectName vs. weight

/*** @returns a map containing ObjectNames of the childNodes and it weight in Integer form */
	public Map getNodeWeights(); //ObjectName vs. weight

/*** @returns a map containing specified ObjectNames of the childNodes and
 its weight in Integer form */
	public Map getNodeWeights(ObjectName[] names); //ObjectName vs. weight

How do I delete Datasource Cluster Resources?

You can delete a DSC only through the use of MBeans. Follow these steps to delete a DSC:

  1. Get the root serverMbean by looking up for root-server-mbean from the naming service. J2eeServerMBean serverMBean = ic.lookUp("root-server-mbean");
  2. Get the resource service MBean from ResourceServiceMbean resourceServiceMBean = serverMBean.getResourceService();.
  3. From this, get the JDBCResourceMBean: JDBCResourceMBean jdbcResource= resourceServiceMBean.getJDBCResourceMBean();
  4. After this, call any of the following methods:public void deleteClusteredDataSource(JDBCClusteredDataSourceMBean clusteredDataSource) throws ResourceException;orpublic void deleteClusteredDataSource(ObjectName clusteredDataSourceObjectName) throws ResourceException;

Related Topics:

© Pramati Technologies 2007 Runs on Pramati Server | Feedback | Legal