Home > Java, Sysadmin > Create a JDBC Connection Pool using JMX/AMX

Create a JDBC Connection Pool using JMX/AMX

In my previous post, I demonstrate how to set a property for each http-service in a glassfish domain using groovy and jmx. Here the code to create a new JDBC Connection Pool, still using groovy and JMX/AMX APIs.

 
import javax.management.ObjectName
import javax.management.remote.JMXConnectorFactory as JmxFactory
import javax.management.remote.JMXServiceURL as JmxUrl
import javax.management.MBeanServerConnection
 
 
 
//def uri = 'service:jmx:rmi:///jndi/rmi://portal3:46302/jmxrmi'
def uri = 'service:jmx:rmi:///jndi/rmi://localhost:8686/jmxrmi'
def login = 'admin'
def password = 'adminadmin'
 
import javax.management.Query
 
def jmxurl = new JmxUrl(uri)
def attributes = new Hashtable()
 
def buffer = [ login, password ]
 
attributes.put("jmx.remote.credentials", (String[]) buffer)
 
def server = JmxFactory.connect(jmxurl, attributes).mBeanServerConnection
 
def createJDBCConnectionPool = { name ->
    def objectname = new ObjectName("amx:j2eeType=X-DomainConfig,name=na")
    def options = new ArrayList()
    options.add(name)
    options.add("com.mysql.jdbc.jdbc2.optional.MysqlDataSource")
    def properties = [
        'ResType':'javax.sql.DataSource'
    ]
 
    options.add(properties as Map)
 
    server.invoke(objectname, 
                "createJDBCConnectionPoolConfig",
                options as Object[],
                ["java.lang.String","java.lang.String","java.util.Map"] as String[] )
 
    /* To add other properties, we need to invoke setProperty to new object */
 
    objectname = new ObjectName("amx:j2eeType=X-JDBCConnectionPoolConfig,name=" + name)
 
    server.invoke(objectname, "setPropertyValue", [ "user", "xwiki"] as Object[], [ "java.lang.String", "java.lang.String"] as String[])
    server.invoke(objectname, "setPropertyValue", [ "password", "xwiki"] as Object[], [ "java.lang.String", "java.lang.String"] as String[])
    server.invoke(objectname, "setPropertyValue", [ "databaseName", "xwiki"] as Object[], [ "java.lang.String", "java.lang.String"] as String[])
    server.invoke(objectname, "setPropertyValue", [ "serverName", "localhost"] as Object[], [ "java.lang.String", "java.lang.String"] as String[])
    server.invoke(objectname, "setPropertyValue", [ "port", "3306"] as Object[], [ "java.lang.String", "java.lang.String"] as String[])
    server.invoke(objectname, "setPropertyValue", [ "networkProtocol", "tcp"] as Object[], [ "java.lang.String", "java.lang.String"] as String[])
}
 
createJDBCConnectionPool("xwikiPool")

As you probably noticed, I can’t create properties directly using the invokation of createJDBCConnectionPool. Indeed, we can specifly only few properties (take a look at the complete list). However, once the object is created, we can use the setPropertValue to create any properties we want.

Categories: Java, Sysadmin Tags: ,