티스토리 뷰

http://www.mastertheboss.com/en/jboss-application-server/82-jboss-datasource-ha.html

http://www.jboss.org/community/wiki/JBossJCADatabaseFailover

에서 참조.

This tutorial has been updated for the new release of JBoss Application Server. We'll discuss about High availability Datasources.

A High availability data source is an abstraction around a group of data sources that provides failover processing between a list of data source. High availability data sources are bound to the JNDI tree or local application context just like data sources are bound to the JNDI tree.  

When you configure an HA Datasource you can choose a list of Connection URL- when the Connection you are using is not available anymore, the connection factory will (transparently to the user) choose another database connection URL, try to allocate a connection using it and, in case of success, return the connection back to the user.

jboss HA Datasource 

Configuring an HA Datasource is a matter of a few steps:
 

1. Check that resource adapters are deployed

In order to use HA Datasource you need a specialized version of the Connection factory: depending if you use local or XA transaction you would need the files jboss-ha-local-jdbc.rar and jboss-ha-xa-jdbc.rar packages.

jboss datasource

In recent versions of JBoss this files are already deployed under the "default" configuration. In older release you can find the Resource Adapters under JBOSS_HOME/docs/examples/jca 
 

2. Create the DataSource Configuration file.

The configuration file for an HA datasource isnot much different from an ordinary datasource file, what makes the trick is the "<url-delimiter>" which separates the list of Connection-urls.
So here's a configuration for a Local HA Datasource with an Oracle driver class:
 

  1. <?xml version="1.0" encoding="UTF-8"?>     
  2. <datasources>      
  3.  <ha-local-tx-datasource>        
  4.   <jndi-name>HADefaultDS</jndi-name>        
  5.    <!-- list of connection URLs -->        
  6.    <connection-url>jdbc:oracle:thin:@oraclehost:1521:SID|jdbc:oracle:thin:@oraclehost:1521:SID2         
  7.    </connection-url>         
  8.    <url-delimiter>|</url-delimiter>        
  9.    <!-- The driver class -->        
  10.    <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>        
  11.    <user-name>user</user-name>        
  12.    <password>password</password>         
  13.    <!-- this will be run before a managed connection is removed from the pool for use by a client-->       
  14.    <check-valid-connection-sql>select count(*) from testable</check-valid-connection-sql>     
  15.  </ha-local-tx-datasource>    
  16. </datasources>       

If on the other hand you need to configure an XA HA Datasource, you can use the following template:

  1. <ha-xa-datasource>         
  2.  <jndi-name>XaHADefaultDS</jndi-name>          
  3.  <!-- This disables transaction interleaving (which BTW,most DB vendors don't support) -->       <track-connection-by-tx></track-connection-by-tx>         
  4.  <isSameRM-override-value>false</isSameRM-override-value>         
  5.  <xa-datasource-class>org.jboss.test.jca.adapter.MockedXADataSource</xa-datasource-class>         
  6.  <!-- list of connection URLs -->         
  7.  <xa-datasource-property name="URL">jdbc:oracle:thin:@oraclehost:1521:SID|jdbc:oracle:thin:@oraclehost:1521:SID2</xa-datasource-property>         
  8.  <url-property>URL</url-property>         
  9.  <url-delimeter>|</url-delimeter>         
  10.  <check-valid-connection-sql>VALUES CURRENT TIMESTAMP</check-valid-connection-sql>      
  11. </ha-xa-datasource> 

What happens if one connection fails ?

When a Connection is no longer available JBoss warns you about it and picks up the next Connection from the list using the Round-Robin Algorithm.

15:20:01,234 WARN  [HALocalManagedConnectionFactory] Failed to create connection  for jdbc:oracle:thin:@oraclehost:1521:SID: IO Exception : The Network Adapter could not establish the connection

JBoss 5 High Availability Datasource

JBoss 5 has changed the configuration for HA Datasources. You don't have anymore
<ha-local-tx-datasource> or  <ha-xa-datasource> Element. Simply use a Connection URL with a list of Connections separated by an URL Delimiter.
Here's an example for an XA-Datasource:
 

  1. <datasources> 
  2.   <ha-local-tx-datasource> 
  3.     
  4.     <jndi-name>jdbc/DBConnection</jndi-name> 
  5.     <connection-url>jdbc:oracle:thin:@host1:1521:db1|jdbc:oracle:thin:@host2:1521:db2</connection-url> 
  6.     <url-delimiter>|</url-delimiter> 
  7.  
  8.     <driver-class>oracle.jdbc.driver.OracleDriver</driver-class> 
  9.     <user-name>scott</user-name> 
  10.     <password>tiger</password> 
  11.  
  12.       
  13.     <min-pool-size>5</min-pool-size> 
  14.     <max-pool-size>20</max-pool-size> 
  15.  
  16.     <check-valid-connection-sql>select 1 from dual</check-valid-connection-sql> 
  17.  
  18.     <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name> 
  19.  
  20.     <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name> 
  21.  
  22.       
  23.   </ha-local-tx-datasource> 
  24.  
  25. </datasources> 

Also, in the new Application Server release you don't have any more jboss-ha-local-jdbc.rar and jboss-ha-xa-jdbc.rar.

The logic contained in Classes HALocalManagedConnectionFactory and  HAXAManagedConnectionFactory has been transferred directly in the standard XA/Local ManagedConnectionFactory


Database Failover

Overview

Since version 4.0.2, JBoss supports database failover for local and XA datasources. What does that actually mean? It means that when a user calls javax.sql.DataSource.getConnection() and the connection to the database could not be allocated (or it was pooled and became invalid), the connection factory will (transparently to the user) choose another database connection URL, try to allocate a connection using it and, in case of success, return the connection back to the user.

This functionality is supported for both local datasources and datasources that implement XA interface.

What policy is used to select the next URL during failover?

It's sticky round-robin. It means that first we try the first connection URL in the list. If it worked, we stick to it until it fails. When it fails, we try the second one and if it worked, we stick to it. And so on. When the last URL in the list was tried and it failed, we will come back to the first URL again, then the second and so on. If for a connection request we tried all the URLs in the list and none worked, an exception will be thrown with a message saying that none of the URLs worked.

How to deploy

1. Deploy HA managed connection factories

There are special managed connection factories for datasources that support failover. These connection factories are deployed as resource adapters (RAR packaged). So, the first step is to deploy these resource adapters that are not deployed in the default configuration.

In jboss-4.0.2/docs/examples/jca directory you will find jboss-ha-local-jdbc.rar and jboss-ha-xa-jdbc.rar packages that contain resource adapters for local and XA datasources that support failover. Depending on whether you are going to use local or XA datasources, or both of them, just copy one or both of the packages to the server's deploy directory.

2. Configure the datasource for failover

Both, local and XA datasources, are configured in a -ds.xml file.

2.1. Configure local datasource for failover

The root element for local datasources that support failover is ha-local-tx-datasource which has only one new element that is not present in local-tx-datasource: url-delimeter which contains a string that is used as a delimeter between URLs in connection-url element.

For example:


   <ha-local-tx-datasource>
      <jndi-name>HADefaultDS</jndi-name>

      <!-- list of connection URLs -->
      <connection-url>jdbc:hsqldb:hsql://host1:1702|jdbc:hsqldb:hsql://host2:1703|...</connection-url>
      <url-delimeter>|</url-delimeter>

      <!-- The driver class -->
      <driver-class>org.hsqldb.jdbcDriver</driver-class>
      <!-- The login and password -->
      <user-name>sa</user-name>
      <password></password>

      <!-- this will be run before a managed connection is removed from the pool for use by a client-->
      <check-valid-connection-sql>select count(*) from a</check-valid-connection-sql>
   </ha-local-tx-datasource>

2.2. Configure XA datasource for failover

The root element for XA datasources that support failover is ha-xa-datasource. This element has two additional elements not present in xa-datasource. The first is url-property which defines the name of an xa-datasource-property that contains the list of URLs. And the second one is url-delimeter the definition of which is exactly the same as for the ha-local-tx-datasource.

For example:


   <ha-xa-datasource>
      <jndi-name>MockedHaXaDS</jndi-name>
      <track-connection-by-tx></track-connection-by-tx>
      <isSameRM-override-value>false</isSameRM-override-value>
      <xa-datasource-class>org.jboss.test.jca.adapter.MockedXADataSource</xa-datasource-class>

      <!-- list of connection URLs -->
      <xa-datasource-property name="URL">haXaUrl1|haXaUrl2</xa-datasource-property>

      <url-property>URL</url-property>
      <url-delimeter>|</url-delimeter>
      <check-valid-connection-sql>VALUES CURRENT TIMESTAMP</check-valid-connection-sql>
   </ha-xa-datasource>

Note: both examples include <check-valid-connection-sql>. Checking whether the connection is valid before it is given to the caller is required for transparent failover.

Tests

Testcases for this functionality can be found in JBoss' testsuite in org.jboss.test.jca.test.HAConnectionFactoryUnitTestCase.

4.2 Changes

  As of JBoss 4.2.x the url-delimeter property has been updated to fix the typo. The property is now correctly named url-delimiter.

'데이터베이스 > CUBRID FAQ' 카테고리의 다른 글

CUBRID cast 주의 사항  (0) 2010.01.14
데이터 타입 테스트 케이스  (0) 2010.01.07
auto increment 사용시 주의 사항  (0) 2009.12.22
glo 사용시 loaddb 주의 사항  (0) 2009.11.12