2017-08-16 127 views

回答

1

您可以使用Wildfly的CLI工具來創建數據源,如here所述。如果CLI工具不適合您,則可以直接使用本地管理API,如here所述。

0

以下示例已使用JBoss-EAP 7.0(使用Wildfly)進行了測試。 Pure Wildfly可能會有不同的maven dependency,提供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配置 ,你必須執行的follwowing代碼:

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>