Loading

MuleSoft Database Connector - Spring Bean Based Datasources

Udgivelsesdato: Jan 16, 2025
Opgave

GOAL

Mule comes with a set of out of the box datasources. However sometimes you might need to create a Java DataSource using spring beans for some other database, to customize the configuration or to implement pooling.

Trin

A Spring bean must be created to define the datasource and the referenced from the Database Connector. The datasource points to a class that implements it, that could for example be a pooling wrapper around the JDBC driver. The recommended way is to use an established pool library. For example c3p0 or Apache DBCP, since they are both mature pooled datasource libraries. A version c3p0 0.9.x is provided with Mule since release 3.5.0.

<spring:bean name="SomeDataSource" class="MyPoolingClass" ...>
  <spring:property name="..." value="..."/>
...
</spring:bean>
<db:generic-config name="Generic_Database_Configuration"doc:name="Generic Database Configuration"dataSource-ref="SomeDataSource"/>

Examples

Note that all values are provided just for example. The appropriate or optimal values for each use case should be determined and validated with load testing.

MULE 3

c3p0

The c3p0 configuration of a datasource would look like this:

 
<spring:bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  <spring:property name="driverClass" value="${JDBC.driver}"/>
  <spring:property name="jdbcUrl" value="${JDBC.URL}"/>
  <spring:property name="user" value="${JDBC.user}"/>
  <spring:property name="password" value="${JDBC.password}"/>
  <spring:property name="minPoolSize" value="5"/>
  <spring:property name="maxPoolSize" value="20"/>
  <spring:property name="acquireIncrement" value="1"/>
  <spring:property name="idleConnectionTestPeriod" value="100"/>
  <spring:property name="maxStatements" value="0"/>
  <spring:property name="checkoutTimeout" value="60000"/>
</spring:bean>

DBCP

Using DBCP the bean configuration would look like this:

 
<spring:bean id="jdbcDataSource" name="jdbcConnectionInfo" class="org.apache.commons.dbcp.BasicDataSource" doc:name="Bean">
      <spring:property name="driverClassName" value="${JDBC.driver}"/>
      <spring:property name="url" value="${JDBC.URL}"/>
      <spring:property name="username" value="${JDBC.user}"/>
      <spring:property name="password" value="${JDBC.password}"/>
      <spring:property name="initialSize" value="1"/>
      <spring:property name="maxActive" value="9"/>
      <spring:property name="maxIdle" value="180"/>
 </spring:bean>
 

MULE 4

In Mule 4, the procedure is slightly different since the spring bean needs to be used used with Spring Module
 

- Add the Spring Module to your mule application 

- Be sure you have the following namespace definition and schema reference in your xml 

  1.  xmlns:spring="http://www.mulesoft.org/schema/mule/spring" 
  2.  http://www.mulesoft.org/schema/mule/spring http://www.mulesoft.org/schema/mule/spring/current/mule-spring.xsd" 

- Add Spring Config and replace your DB configuration to the following: 
 
<spring:config name="Spring_Config" doc:name="Spring Config" doc:id="1c56e698-bf68-4120-89f4-8e1dfc026079" files="config/spring-config.xml" /> 
<db:config name="Database_Config" doc:name="Database Config" doc:id="4178ca88-b982-4f90-8043-9f63b2746939"> 
<db:data-source-connection dataSourceRef="dataSource"/> 
</db:config>

- Add the spring resource to your project, in my example is under src/main/resources/config 

- Set the DS as follows (replace with your DB values) in the file config/spring-config.xml (as set in my example) 
 
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 
   <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/> 
   <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:49161:xe"/> 
   <property name="user" value="system"/> 
   <property name="password" value="oracle"/> 
   <property name="minPoolSize" value="1"/> 
   <property name="maxPoolSize" value="25"/> 
   <property name="acquireIncrement" value="5"/> 
   <property name="idleConnectionTestPeriod" value="100"/> 
   <property name="maxStatements" value="10"/> 
   <property name="checkoutTimeout" value="60000"/> 
</bean>

- Add C3P0 dependency and shared library in your POM 
 
<dependency> 
    <groupId>com.mchange</groupId> 
    <artifactId>c3p0</artifactId> 
    <version>0.9.5.2</version> 
</dependency>
 
<plugin>
  <groupId>org.mule.tools.maven</groupId>
  <artifactId>mule-maven-plugin</artifactId>
  <version>${mule.maven.plugin.version}</version>
  <extensions>true</extensions>
  <configuration>
  <sharedLibraries>

   <sharedLibrary> 
     <groupId>com.mchange</groupId> 
     <artifactId>c3p0</artifactId> 
   </sharedLibrary>

  ...
  </sharedLibraries>
...

Please find linked the testdb.jar example for further reference.
 
For a sample Spring bean configuration for Oracle datasource, refer to the article below:
 
For a sample Spring bean configuration for DB2 datasource, refer to the article below:
 
Yderligere ressourcer
testdb.jar
Vidensartikelnummer

001115149

 
Indlæser
Salesforce Help | Article