在 Websphere 中创建 oracle 数据源的 Jython 脚本

Jython script to create oracle datasource in Websphere

以下代码创建数据源成功,但调用测试连接时出现异常。但是当我重新启动服务器并在控制台中使用测试连接时,它就可以工作了。

如何避免服务器重启并使测试连接在脚本中生效。 我在脚本执行后和重启前确实有这个错误:

J2CA0130I: Data Source [nMINEDB] has Component-managed Authentication Alias [test/nMINEDBUser] but no corresponding J2C Authentication Data Entry is defined in security.xml. This may have undesirable effects.

代码:

#
# Jython Script to create MINE web data-source and JDBC provider. 
#

#Import Statements
import os
import re
import sys

# Create JDBC provider for MINE oracle database.
def createMINEJDBCProvider():
    server = '/Server:server1'
    # Set the Node ID
    serverID = AdminConfig.getid(server)
    print 'Server ID:' + serverID

    #Configuring J2c auth
    userAlias='test/nMINEDBUser'
        alias = ['alias', userAlias]
    userid = ['userId', 'MINEDB']
    password = ['password', 'MINEpass']
    jaasAttrs = [alias, userid, password]
    security = AdminConfig.getid('/Security:/')
    print 'security:'+security
    j2cUser=AdminConfig.create('JAASAuthData', security, jaasAttrs)
    AdminConfig.save()
    print 'Creating MINE User sucessfull'

    # Test to see if the provider has already been created.
    MINEJDBCprovider = AdminConfig.getid('/JDBCProvider:nOracle JDBC Driver/')
    if len(MINEJDBCprovider) == 0:
        providerName='nOracle JDBC Driver'
        print 'creating Oracle JDBC provider on server:'+serverID
        print 'JDBC provider Name:'+providerName
        MINEJDBCprop1 = ['name', providerName]
        MINEJDBCprop2 = ['description','Oracle JDBC Driver for MINE Application']
        MINEJDBCprop3 = ['implementationClassName','oracle.jdbc.pool.OracleConnectionPoolDataSource']
        MINEJDBCprop4 = ['classpath','C:/jars/ojdbc/ojdbc6.jar']
        MINEJDBCprops=[MINEJDBCprop1,MINEJDBCprop2,MINEJDBCprop3,MINEJDBCprop4]
        providerID = AdminConfig.create('JDBCProvider', serverID, MINEJDBCprops)
        AdminConfig.save()
        print 'Creating Oracle JDBC provider on server sucessfull with provider:'+providerID
        createMINEDataSource()
    else:
        print 'oracle provider exists:'+MINEJDBCprovider


def createMINEDataSource():
    providerName='nOracle JDBC Driver'
    userAlias='test/nMINEDBUser'
    MINEJDBCprovider = AdminConfig.getid('/JDBCProvider:nOracle JDBC Driver/')
    MINEDataSource = AdminConfig.getid('/JDBCProvider:'+providerName+'/DataSource:MINEDB/')
    if len(MINEDataSource) == 0:
        # Set the datasource attributes
            MINEDSprop1 = ['name', 'nMINEDB']
            MINEDSprop2 = ['jndiName', 'jdbc/nMINEdb']
            MINEDSprop3 = ['description', 'MINE database']
            MINEDSprop4 = ['datasourceHelperClassname', 'com.ibm.websphere.rsadapter.Oracle11gDataStoreHelper']
            MINEDSprop5 = ['authDataAlias' , userAlias]
            mapConfigprop=["mappingConfigAlias", "DefaultPrincipalMapping"] 
        mapConfigs=[MINEDSprop5 , mapConfigprop] 
        mappingConfig=["mapping", mapConfigs]

            MINEDSprops = [MINEDSprop1, MINEDSprop2, MINEDSprop3, MINEDSprop4, MINEDSprop5, mappingConfig]
            MINEDataSource = AdminConfig.create('DataSource', MINEJDBCprovider, MINEDSprops)

            #Set the DB URL
            propSet = AdminConfig.create('J2EEResourcePropertySet', MINEDataSource, [])
            AdminConfig.create('J2EEResourceProperty', propSet, [["name", "URL"], ["value", "jdbc:oracle:thin:@myserver:1523:MINED2"]])

            AdminConfig.save()


                        print 'Creating MINE JDBC Datasource on server sucessfull with datasource:'+MINEDataSource

        #Mapping module not trying right now
        #AdminConfig.create('MappingModule', MINEDataSource, mappingConfig)



    else:
        print 'MINE Datasource already exists in the server:'+MINEDataSource
    print 'Testing datasource connection'
    print AdminControl.testConnection(MINEDataSource)

try:

    print 'start'
    createMINEJDBCProvider()
    createMINEDataSource()
    print 'end'
except:
    print "***** Unexpected error while creating JDBC datasource:", sys.exc_info(), " *****"
    raise

j2c 身份验证在服务器被回收之前不会初始化。