Home

 

Creating a data source

We create a function named createDS for this purpose:

def createDS():

The following code snippet checks if the data source we want to create already exists, and if it does, then we simply return. Once again, make sure that rest of the code for this method is indented:
dsId = AdminConfig.getid("/JDBCProvider:"+jdbcProv + "/DataSource:"+
				dataSourceName + "/")
if len(dsId) > 0:
	return 
#endif

Define local variables for attributes required to create a data source:
dsname=['name',dataSourceName]
jndiName=['jndiName',jndiDS]
description=['description','ITSOBank Data Source']	
dsHelperClassname=['datasourceHelperClassname',
						'com.ibm.websphere.rsadapter.DerbyDataStoreHelper']
dsAttrs=[]
dsAttrs.append(dsname)
dsAttrs.append(jndiName)
dsAttrs.append(description)
dsAttrs.append(dsHelperClassname)
provId = AdminConfig.getid("/Node:"+nodeName + "/Server:"+srvr +
						"/JDBCProvider:"+jdbcProv + "/")

Create the data source and save the configuration:
AdminConfig.create('DataSource',provId,dsAttrs)
AdminConfig.save()

We have to configure the data source and add resource property set attributes, such as database name, password, description, and login timeout. Because this piece of code is more than a few lines, we put the code into a separate function. Let us define a function call:
modifyDS()

The last step is to enable the use of this data source in container-managed persistence. We use a separate method as well:
useDSinCMP()

The code for this function is shown in...

Example 26-3 Create a data source

def createDS():
	dsId = AdminConfig.getid("/JDBCProvider:"+jdbcProv + "/DataSource:"+
					dataSourceName + "/")
	if len(dsId) > 0:
		return 
	#endif
	dsname=['name',dataSourceName]
	jndiName=['jndiName',jndiDS]
	description=['description','ITSOBank Data Source']	
	dsHelperClassname=['datasourceHelperClassname',
						'com.ibm.websphere.rsadapter.DerbyDataStoreHelper']
	dsAttrs=[]
	dsAttrs.append(dsname)
	dsAttrs.append(jndiName)
	dsAttrs.append(description)
	dsAttrs.append(dsHelperClassname)
	provId = AdminConfig.getid("/Node:"+nodeName + "/Server:"+srvr +
						"/JDBCProvider:"+jdbcProv + "/")
		AdminConfig.create('DataSource',provId,dsAttrs)
	AdminConfig.save()
	modifyDS()  | 160;# modify DS to add the poperties (databaseName) 
	useDSinCMP() # enable this DS in Container Managed Persistence (CMP)
#enddef

ibm.com/redbooks