Programmatically creating datasource using dmr hangs
yangju Aug 23, 2018 3:08 PMHere is my code:
public void createDatasourceProgrammatically() throws Exception{
ModelNode op = new ModelNode();
ModelControllerClient client = null;
try {
op.get(ClientConstants.OP).set(ClientConstants.ADD);
op.get(ClientConstants.OP_ADDR).add("subsystem", "datasources");
op.get(ClientConstants.OP_ADDR).add("data-source", "java:jboss/datasources/ds1");
op.get("jndi-name").set("java:jboss/datasources/ds1");
op.get("connection-url").set("jdbc:mysql://mydomain:3306/ds1?zeroDateTimeBehavior=convertToNull&useUnicode=true&connectionCollation=utf8_general_ci&characterSetResults=utf8&characterEncoding=utf8&useInformationSchema=true&noAccessToProcedureBodies=true&autoReconnect=true&autoReconnectForPools=true");
op.get("driver-class").set("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource");
op.get("driver-name").set("com.mysql");
op.get("user-name").set("aaa");
op.get("password").set("bbb");
op.get("pool-name").set("ds1");
client = createClient(InetAddress.getByName("127.0.0.1"), 9990, "admin","admin","ManagementRealm");
ModelNode returnVal = client.execute(op);
}
catch(Exception e) {
log.error("faile to execute", e);
}
finally{
if(client!=null){
client.close();
}
}
}
private ModelControllerClient createClient(InetAddress host, int port,
String username, String password, String securityRealmName) {
final CallbackHandler callbackHandler = new CallbackHandler() {
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback current : callbacks) {
if (current instanceof NameCallback) {
NameCallback ncb = (NameCallback) current;
ncb.setName(username);
} else if (current instanceof PasswordCallback) {
PasswordCallback pcb = (PasswordCallback) current;
pcb.setPassword(password.toCharArray());
} else if (current instanceof RealmCallback) {
RealmCallback rcb = (RealmCallback) current;
rcb.setText(rcb.getDefaultText());
} else {
throw new UnsupportedCallbackException(current);
}
}
}
};
return ModelControllerClient.Factory.create(host, port, callbackHandler);
}
When I ran above code in wildfly 11.0.0.Final server, the execution of client.execute(op) hangs forever.
I use the similar code but just to do some read operation, and it returns result immediately.
Maven dependencies:
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jboss-dmr</artifactId>
<version>1.4.1.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly.core</groupId>
<artifactId>wildfly-controller-client</artifactId>
<version>3.0.8.Final</version>
<scope>provided</scope>
</dependency>
Any help is appreciated.