Infrastructure at your Service

Pascal Brand

Using LDAP resolved connection descriptor in WebLogic JDBC data sources.

I got the question if it is possible to have a the WebLogic JDBC Data Source to be resolved by an LDAP entry. The answer is yes; since WebLogic 12.2.1.3 a JDBC connection pool URL declaration can point to an LDAP entry.

This can be done by simply editing an existing JDBC data source.

jdbc:oracle:thin:@ldap://vm01.dbi-workshop.com:1389/cn=orcl,cn=OracleContext,dc=DatabaseConnection,dc=com

WebLogic_JDBC_Connection_pool

Of course the LDAP server needs to have been configured to store the TNS entries. I explained how to do this in one of my previous blogs: how-to-declare-tns-entries-in-oracle-unified-directory-oud
Or it can be done using WLST scripts.
First a properties file

 DS_NAME=MyLDAPDatasource
JNDIName=jdbc/MyLDAPDatasource
TEST_TABLE_QUERY=SQL SELECT 1 FROM DUAL
JDBC_DRIVER=oracle.jdbc.OracleDriver
TARGET=Server1
JDBC_URL=jdbc:oracle:thin:@ldap://vm01.dbi-workshop.com:1389/cn=orcl,cn=OracleContext,dc=DatabaseConnection,dc=com
DB_USER=USER01
DB_USER_PASSWORD=Welcome1
POOL_MIN_CAPACITY=10
POOL_MAX_CAPACITY=100
POOL_INITIAL_CAPACITY=1
POOL_STATEMENT_CACHE=10           
XA_TRANSACTION_TIMEOUT=7200
XA_RETRY_INTERVAL_SECONDS=60
XA_RETRY_DURATION_SECONDS=300
JDBC_DEBUG_LEVEL=10

and then the python script

#read the domain properties file
try:
  print "Load properties file"
  properties =  os.environ["WEBLOGIC_DOMAIN_DEF_DIR"] + "/" + os.environ["WEBLOGIC_DOMAIN"] + "/domain.properties"
  print properties
  loadProperties(properties)
except :
  print "unable to load domain.properties file"
#  exit(exitcode=1)

try:
  jdbcProperties=os.path.realpath(os.path.dirname(sys.argv[0])) + "/JDBC_Datasource.properties"
  print jdbcProperties
  loadProperties(jdbcProperties)

except :
  print "Unable to load JDBC_Camunda.properties"
  exit(exitcode=1)

#AdminUser=raw_input('Please Enter WebLogic Domain Admin user Name: ')
#AdminPassword= "".join(java.lang.System.console().readPassword("%s", ['Please enter WebLogic Domain Admin user password:']))


try:
    #Connect to AdminServer
    connect(userConfigFile=CONFIG_FILE,userKeyFile=KEY_FILE,url=ADMIN_URL)
    #connect(url=ADMIN_URL)
    #connect(AdminUser,AdminPassword,ADMIN_URL)
    #connect()
    #connect('weblogic','Welcome1')
except:
    print "Unable to connect"
    exit(exitcode=1)
	
try: 
    edit()
    startEdit()

    cd('/')
    cmo.createJDBCSystemResource(DS_NAME)
    cd('/JDBCSystemResources/'+DS_NAME+'/JDBCResource/'+DS_NAME)
    cmo.setName(DS_NAME)
    cd('/JDBCSystemResources/'+DS_NAME+'/JDBCResource/'+DS_NAME+'/JDBCDataSourceParams/'+DS_NAME)

    print "Setting JNDI Names"
    set('JNDINames',jarray.array([String(JNDIName)], String))
    cd('/JDBCSystemResources/'+DS_NAME+'/JDBCResource/'+DS_NAME)
    cmo.setDatasourceType('GENERIC')
    cd('/JDBCSystemResources/'+DS_NAME+'/JDBCResource/'+DS_NAME+'/JDBCDriverParams/'+DS_NAME)

    print "Setting JDBC URL"
    cmo.setUrl(JDBC_URL)

    print "Setting Driver Name"
    cmo.setDriverName(JDBC_DRIVER)
    
    print "Setting Password"
    set('Password', DB_USER_PASSWORD)
    cd('/JDBCSystemResources/'+DS_NAME+'/JDBCResource/'+DS_NAME+'/JDBCConnectionPoolParams/'+DS_NAME)
    cmo.setTestTableName(TEST_TABLE_QUERY)
    cd('/JDBCSystemResources/'+DS_NAME+'/JDBCResource/'+DS_NAME+'/JDBCDriverParams/'+DS_NAME+'/Properties/'+DS_NAME)
    cmo.createProperty('user')
    cd('/JDBCSystemResources/'+DS_NAME+'/JDBCResource/'+DS_NAME+'/JDBCDriverParams/'+DS_NAME+'/Properties/'+DS_NAME+'/Properties/user')
    cmo.setValue(DB_USER)

    cd('/JDBCSystemResources/'+DS_NAME)
    set('Targets',jarray.array([ObjectName('com.bea:Name='+TARGET+',Type=Server')], ObjectName))
   
    print "Saving and activating changes"
    save()    
    activate()

except Exception, e:
    dumpStack()
    print "ERROR 2... check error messages for cause."
    print e
    dumpStack()
    stopEdit(defaultAnswer='y')
    exit(exitcode=1)
	
try: 
    edit()
    startEdit()

    cd('/JDBCSystemResources/'+DS_NAME+'/JDBCResource/'+DS_NAME+'/JDBCDriverParams/'+DS_NAME+'/Properties/'+DS_NAME+'/Properties/user')
    cmo.unSet('SysPropValue')
    cmo.unSet('EncryptedValue')
    cmo.setValue(DB_USER)
    cd('/JDBCSystemResources/'+DS_NAME+'/JDBCResource/'+DS_NAME+'/JDBCConnectionPoolParams/'+DS_NAME)
    cmo.setInitialCapacity(long(POOL_INITIAL_CAPACITY))
    cmo.setMinCapacity(long(POOL_MIN_CAPACITY))
    cmo.setStatementCacheSize(long(POOL_STATEMENT_CACHE))
    cmo.setMaxCapacity(long(POOL_MAX_CAPACITY))
    cmo.setStatementCacheType('LRU')
    cd('/JDBCSystemResources/'+DS_NAME+'/JDBCResource/'+DS_NAME+'/JDBCDataSourceParams/'+DS_NAME)
    cmo.setGlobalTransactionsProtocol('OnePhaseCommit')
       
    save()
    activate()

except Exception, e:
    print "ERROR... check error messages for cause."
    print e
    stopEdit(defaultAnswer='y')
    exit(exitcode=1)
	
exit(exitcode=0)

This script and properties file can be used to create the JDBC connection on one WebLogic Server defined as TARGET in the properties file.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pascal Brand
Pascal Brand

Senior Consultant & Middleware Technology Leader