+

Search Tips   |   Advanced Search

Example: Creating a JDBC provider and data source using Java Management Extensions API and the scripting tool


The following sample code is a JACL (wsadmin - scripting tool) script used to create a data source.

Use this script to create only data sources for which WAS does not provide a template. For every JDBC provider WAS supports, WAS provides a corresponding data source template. See the topic, Creating configuration objects using wsadmin, for instructions on how to use the createUsingTemplate command to establish these data sources. For a complete list of supported JDBC providers (and therefore a complete list of data sources that must be created using a template), refer to the topic, Data source minimum required settings, by vendor.

This script sets up the following sample JDBC objects:

If we later modify the class path or native library path of the JDBC provider associated with the data source, restart every appserver within the scope of that JDBC provider for the new configuration to work. Otherwise, you receive a data source failure message.

# AWE --  Set up XA DB2 data sources, 
# both V4.0 and  Connector architecture (JCA)-compliant data sources


#UPDATE THESE VALUES:
#The classpath that will be used by the database driver 
set driverClassPath "c:/sqllib/java/db2java.zip"
set server "server1"
set fvtbase "c:/wssb/fvtbase"

#Users and passwords.. 
set defaultUser1 "dbuser1" 
set defaultPassword1 "dbpwd1" 
set aliasName "alias1"
set databaseName1 "jtest1" 
set databaseName2 "jtest2"
#END OF UPDATES
 puts "Add an alias alias1" 
set cell [$AdminControl getCell]   
set sec [$AdminConfig getid /Cell:$cell/Security:/]

#---------------------------------------------------------
# Create a JAASAuthData object for component-managed authentication 
#--------------------------------------------------------- 
puts "create JAASAuthData object for alias1" 
set alias_attr [list alias $aliasName] 
set desc_attr     [list description "Alias 1"] 
set userid_attr   [list userId $defaultUser1] 
set password_attr [list password $defaultPassword1] 
set attrs [list $alias_attr $desc_attr $userid_attr $password_attr]
set authdata [$AdminConfig  create JAASAuthData $sec $attrs] 
$AdminConfig save
 puts "Installing DB2 datasource for XA"
 puts "Finding the old JDBCProvider.."
#Remove the old jdbc provider... 
set jps [$AdminConfig list JDBCProvider] 
foreach jp $jps {
set jpname [lindex [lindex [$AdminConfig show $jp {name}] 0] 1]
 if {($jpname == "FVTProvider")} {
  puts "Removing old JDBC Provider"
  $AdminConfig remove $jp
  $AdminConfig save
 }
}


#Get the server name... 
puts "Finding the server $server" 
set servlist [$AdminConfig list Server] 
set servsize [llength $servlist] 
foreach srvr $servlist {
 set sname [lindex [lindex [$AdminConfig show $srvr {name}] 0] 1]
 if {($sname == $server)} {
  puts "Found server $srvr"
  set serv $srvr
 }
}
set desiredNodeName "myNode" 
puts "Finding the Node" 
set nodelist [$AdminConfig list Node] 
foreach node $nodelist {
  set nodename [lindex [lindex [$AdminConfig show $node {name}] 0] 1]
  if {($nodename == $desiredNodeName)} {
    puts "node = $node"
    break
  }
}  
 puts "Finding the Resource Adapter" 
set ralist [$AdminConfig list J2CResourceAdapter $node] 
set ralistlen [llength $ralist] 
foreach ra $ralist {
  set raname [lindex [lindex [$AdminConfig show $ra {name}] 0] 1]
  if {($raname == "WebSphere Relational Resource Adapter")} {
    set rsadapter $ra
  }
}


#Create a JDBC Provider for the data sources 
puts "Creating the provider for COM.ibm.db2.jdbc.DB2XADataSource" 
set attrs1 [subst {{classpath $driverClassPath} 
  {implementationClassName COM.ibm.db2.jdbc.DB2XADataSource}{name "FVTProvider2"} 
  {description "DB2 JDBC Provider"} {xa "true"}}] 
set provider1 [$AdminConfig create JDBCProvider $serv $attrs1]

#Create the first data source 
puts "Creating the datasource fvtDS_1" 
set attrs2 [subst {{name fvtDS_1} {description "FVT DataSource 1"}}] 
set ds1 [$AdminConfig create DataSource $provider1  $attrs2]

#Set the properties for the data source. 
set propSet1 [$AdminConfig create J2EEResourcePropertySet $ds1 {}]
set attrs3 [subst {{name databaseName} {type java.lang.String} {value $databaseName1}}]
$AdminConfig create J2EEResourceProperty $propSet1 $attrs3
 set attrs10 [subst {{jndiName jdbc/fvtDS_1} {statementCacheSize 10} 
  {datasourceHelperClassname com.ibm.websphere.rsadapter.DB2DataStoreHelper} 
  {relationalResourceAdapter {$rsadapter}} {authMechanismPreference "BASIC_PASSWORD"} 
  {authDataAlias  $aliasName}}]
$AdminConfig modify $ds1 $attrs10


#Create the connection pool object
$AdminConfig create ConnectionPool $ds1 {{connectionTimeout 1000} 
  {maxConnections 30} {minConnections 1} {agedTimeout 1000} 
  {reapTime 2000} {unusedTimeout 3000} }


#Create the 4.0 data sources 
puts "Creating the 4.0 datasource fvtDS_3" 
set ds3 [$AdminConfig create WAS40DataSource $provider1 {{name fvtDS_3} {description "FVT 4.0 DataSource"}}]

#Set the properties on the data source 
set propSet3 [$AdminConfig create J2EEResourcePropertySet $ds3 {}]

#These attributes should be the same as fvtDS_1 
set attrs4 [subst {{name user} {type java.lang.String} {value $defaultUser1}}] 
set attrs5 [subst {{name password} {type java.lang.String} {value $defaultPassword1}}]
$AdminConfig create J2EEResourceProperty $propSet3 $attrs3
$AdminConfig create J2EEResourceProperty $propSet3 $attrs4
$AdminConfig create J2EEResourceProperty $propSet3 $attrs5 
set attrs10 [subst {{jndiName jdbc/fvtDS_3} {databaseName $databaseName1}}]
$AdminConfig modify $ds3 $attrs10

$AdminConfig create WAS40ConnectionPool $ds3 {{orphanTimeout 3000} {connectionTimeout 1000} 
  {minimumPoolSize 1} {maximumPoolSize 10} {idleTimeout 2000}}

#Add a CMP connection factory for the JCA-compliant data source. This step is not necessary for 
#Version 4 data sources, as they contain built-in CMP connection factories. 
puts "Creating the CMP Connector Factory for fvtDS_1" 
set attrs12 [subst {{name "FVT DS 1_CF"} {authMechanismPreference BASIC_PASSWORD} 
  {cmpDatasource $ds1} {authDataAlias $aliasName}}] 
set cf1 [$AdminConfig create CMPConnectorFactory $rsadapter $attrs12]

#Set the properties for the data source.
$AdminConfig create MappingModule $cf1 {{mappingConfigAlias "DefaultPrincipalMapping"} {authDataAlias "alias1"}}

$AdminConfig save




Related concepts


Relational resource adapters and JCA
JDBC providers
Data sources
Connection pooling

 

Related tasks


Create configuration objects using wsadmin

 

Related