Weblogic Datasources brief overview along with WLST examples

Overview:

WebLogic datasources are used to manage connections to databases, which are essential for most enterprise applications. A datasource is a pool of database connections that can be shared by multiple applications. When an application requests a connection from the datasource, it is provided with a connection from the pool, and when the application is done with the connection, it returns it to the pool.

WebLogic datasources provide several benefits, such as connection pooling, load balancing, and failover support. Connection pooling ensures that database connections are efficiently managed and reused, which can result in improved application performance. Load balancing allows requests to be distributed across multiple database servers, which can help prevent overload on any one server. Failover support ensures that if a database server becomes unavailable, requests are automatically routed to a backup server.

WebLogic datasources can be configured and managed through the WebLogic console, WLST scripting, or using third-party tools. Configuration options include the number of connections in the pool, connection timeout settings, and load balancing and failover settings. Monitoring options include tracking connection usage, connection pool statistics, and database server status.

Proper tuning of WebLogic datasources is essential for maintaining optimal performance and availability of enterprise applications. Best practices for datasource tuning include adjusting the number of connections in the pool based on expected application usage, configuring connection timeouts to avoid unnecessary connection pool wait times, and setting appropriate load balancing and failover settings to ensure proper distribution of requests and database server availability.

Weblogic Datasource creation using WLST

The below script creates a JDBC datasource named MyDataSource using an Oracle JDBC driver. It sets the JNDI name to jdbc/MyDataSource, the connection URL to jdbc:oracle:thin:@localhost:1521:xe, and the maximum and initial pool sizes to 20 and 2, respectively. It also sets the user and password properties and enables two-phase commit for global transactions.

connect('weblogic','password','t3://localhost:7001')
edit()
startEdit()

cd('/')
cmo.createJDBCSystemResource('MyDataSource')

cd('/JDBCSystemResources/MyDataSource/JDBCResource/MyDataSource')
cmo.setName('MyDataSource')

cd('/JDBCSystemResources/MyDataSource/JDBCResource/MyDataSource/JDBCDataSourceParams/MyDataSource')
set('JNDINames',jarray.array([String('jdbc/MyDataSource')], String))

cd('/JDBCSystemResources/MyDataSource/JDBCResource/MyDataSource/JDBCDriverParams/MyDataSource')
cmo.setUrl('jdbc:oracle:thin:@localhost:1521:xe')
cmo.setDriverName('oracle.jdbc.OracleDriver')
cmo.setPassword('password')
 
cd('/JDBCSystemResources/MyDataSource/JDBCResource/MyDataSource/JDBCConnectionPoolParams/MyDataSource')
cmo.setMaxCapacity(20)
cmo.setInitialCapacity(2)

cd('/JDBCSystemResources/MyDataSource/JDBCResource/MyDataSource/JDBCDriverParams/MyDataSource/Properties/MyDataSource')
cmo.createProperty('user')
cd('/JDBCSystemResources/MyDataSource/JDBCResource/MyDataSource/JDBCDriverParams/MyDataSource/Properties/MyDataSource/Properties/user')
cmo.setValue('weblogic')

cd('/JDBCSystemResources/MyDataSource/JDBCResource/MyDataSource/JDBCDataSourceParams/MyDataSource')
cmo.setGlobalTransactionsProtocol('TwoPhaseCommit')

save()
activate()

exit()

WLST script examples for WebLogic datasource tuning

Set initial capacity of datasource connection pool:

This WLST script sets the initial capacity of the connection pool for a datasource named MyDS to 20.

connect('username','password','t3://localhost:7001')
edit()
startEdit()
cd('/JDBCSystemResources/MyDS/JDBCResource/MyDS/ConnectionPoolParams/MyDS')
cmo.setInitialCapacity(20)
save()
activate()

Set maximum capacity of datasource connection pool:

This WLST script sets the maximum capacity of the connection pool for a datasource named MyDS to 100.

connect('username','password','t3://localhost:7001')
edit()
startEdit()
cd('/JDBCSystemResources/MyDS/JDBCResource/MyDS/ConnectionPoolParams/MyDS')
cmo.setMaxCapacity(100)
save()
activate()

Set the inactive connection timeout of a datasource:

This WLST script sets the inactive connection timeout of a datasource named MyDS to 1200 seconds (20 minutes).

connect('username','password','t3://localhost:7001')
edit()
startEdit()
cd('/JDBCSystemResources/MyDS/JDBCResource/MyDS/ConnectionPoolParams/MyDS')
cmo.setInactiveConnectionTimeoutSeconds(1200)
save()
activate()

Set the test connection on reserve timeout of a datasource:

This WLST script enables test connection on reserve for a datasource named MyDS and sets the test connection on reserve timeout to 60 seconds.

connect('username','password','t3://localhost:7001')
edit()
startEdit()
cd('/JDBCSystemResources/MyDS/JDBCResource/MyDS/ConnectionPoolParams/MyDS')
cmo.setTestConnectionOnReserve(true)
cmo.setTestConnectionsOnReserveTimeout(60)
save()
activate()

Set the shrink frequency of a datasource:

This WLST script sets the shrink frequency of a datasource named MyDS to 900 seconds (15 minutes).

connect('username','password','t3://localhost:7001')
edit()
startEdit()
cd('/JDBCSystemResources/MyDS/JDBCResource/MyDS/ConnectionPoolParams/MyDS')
cmo.setShrinkFrequencySeconds(900)
save()
activate()

Set the statement cache size of a datasource:

This WLST script sets the statement cache size of a datasource named MyDS to 100.

connect('username','password','t3://localhost:7001')
edit()
startEdit()
cd('/JDBCSystemResources/MyDS/JDBCResource/MyDS')
cmo.setStatementCacheSize(100)
save()
activate()

Set the statement timeout of a datasource:

This WLST script sets the statement timeout of a datasource named MyDS to 60 seconds.

connect('username','password','t3://localhost:7001')
edit()
startEdit()
cd('/JDBCSystemResources/MyDS/JDBCResource/MyDS')
cmo.setStatementTimeout(60)
save()
activate()

Set the connection reserve timeout of a datasource:

This WLST script sets the connection reserve timeout of a datasource named MyDS to 60 seconds.

connect('username','password','t3://localhost:7001')
edit()
startEdit()
cd('/JDBCSystemResources/MyDS/JDBCResource/MyDS')
cmo.setConnectionReserveTimeoutSeconds(60)
save()
activate()

Cheers!!!

About the author

Mohit Chaudhary

Hey there, I am IT enthusiast who is passionate about Middleware, DevOps, Cloud and much more.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *