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.
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"/>
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.
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>
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>
In Mule 4, the procedure is slightly different since the spring bean needs to be used used with Spring Module.
- Be sure you have the following namespace definition and schema reference in your xml
<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>
<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>
<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.001115149

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.