如何在Wildfly 10中以编程方式创建数据源?

时间:2017-08-16 03:13:10

标签: java-ee jboss datasource wildfly

我已经在Wildfly中搜索了创建数据源的替代方法,但我没有找到以编程方式创建数据源的替代方法,是否可能?

2 个答案:

答案 0 :(得分:3)

以下示例使用JBoss-EAP 7.0(使用Wildfly)进行测试。 Pure Wildfly可能具有不同的maven依赖关系,提供ModelControllerClient。此示例使用以下Maven依赖关系(来自存储库https://maven.repository.redhat.com/ga):

<dependency>
  <groupId>org.wildfly.core</groupId>
  <artifactId>wildfly-controller-client</artifactId>
  <version>2.1.13.Final-redhat-1</version>
</dependency>

我假设您已经手动配置了数据源并且驱动程序模块已就位jboss/modules/system/layers,以下示例使用MySQL驱动程序。将驱动程序添加到JBoss配置中 你必须执行以下代码:

ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getByName("localhost"), 9990);

ModelNode request = new ModelNode();
request.get(ClientConstants.OP).set(ClientConstants.ADD);
request.get(ClientConstants.OP_ADDR).add("subsystem","datasources");
request.get(ClientConstants.OP_ADDR).add("jdbc-driver","mysql");
request.get("driver-name").set("mysql");
request.get("driver-module-name").set("com.mysql");
request.get("driver-xa-datasource-class-name").set("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource");
client.execute(new OperationBuilder(request).build());
client.close();

这会将驱动程序添加到您的配置中:

<driver name="mysql" module="com.mysql">
  <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>

添加数据源与添加驱动程序非常相似:

ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getByName("localhost"), 9990);

ModelNode request = new ModelNode();
request.get(ClientConstants.OP).set(ClientConstants.ADD);
request.get(ClientConstants.OP_ADDR).add("subsystem","datasources");
request.get(ClientConstants.OP_ADDR).add("data-source","TestDs");
request.get("jta").set(true);
request.get("jndi-name").set("java:jboss/datasources/TestDs");
request.get("pool-name").set("TestDs");
request.get("use-java-context").set(true);
request.get("use-ccm").set(true);

request.get("connection-url").set("jdbc:mysql://localhost/test");
request.get("driver-name").set("mysql");
request.get("transaction-isolation").set("TRANSACTION_READ_COMMITTED");

request.get("min-pool-size").set(5);
request.get("max-pool-size").set(20);
request.get("pool-prefill").set(true);
request.get("pool-use-strict-min").set(false);
request.get("flush-strategy").set("FailingConnectionOnly");

request.get("user-name").set("test");
request.get("password").set("test");

request.get("prepared-statements-cache-size").set(32);
request.get("share-prepared-statements").set(true);

client.execute(new OperationBuilder(request).build());
client.close();

使用此代码,以编程方式创建数据源:

<datasource jta="true" jndi-name="java:jboss/datasources/TestDs" pool-name="TestDs" use-java-context="true" use-ccm="true">
  <connection-url>jdbc:mysql://localhost/test</connection-url>
  <driver>mysql</driver>
  <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
  <pool>
    <min-pool-size>5</min-pool-size>
    <max-pool-size>20</max-pool-size>
    <prefill>true</prefill>
    <use-strict-min>false</use-strict-min>
    <flush-strategy>FailingConnectionOnly</flush-strategy>
  </pool>
  <security>
    <user-name>test</user-name>
    <password>test</password>
  </security>
  <statement>
    <prepared-statement-cache-size>32</prepared-statement-cache-size>
    <share-prepared-statements>true</share-prepared-statements>
  </statement>
</datasource>

答案 1 :(得分:1)

您可以使用Wildfly的CLI工具创建数据源,如here所述。如果CLI工具不够用,您可以直接使用本机管理API,例如here所述。