zappee
zappee

Reputation: 22668

How to use connection pool with Spring Boot + Mybatis

I am learning Spring Boot and I have created a simple application.

I use the following maven dependencies:

I would like to use connection pool to increase the speed of my application but I am not sure how to configure Spring Boot and MyBatis for this use case.

Before that I used Java EE + application server and I configured mybatis via mybatis.xml file to use JNDI data source. The connection pool was provided by application server.

But now, I am a little bit confused where to put database connection parameters because there are two possible candidates: application.properties and mybatis.xml.

What is the connection between these two config files in this environment?

I know Spring Boot application runs on Tomcat web container so I am not able to create JNDI data source and connection pool in it.

What is the proper way to specify database connection + connection pool when I use Spring Boot with Mybatis?

Currently I added the connection parameters to mybatis.xml and application.xml file as well:

application.properties

spring.datasource.url=...
spring.datasource.username=...
spring.datasource.password=...
spring.datasource.driver-class-name=...

mybatis.xml

<configuration>
    <environments default="jdbc">
        <environment id="jdbc">
            <transactionManager type="JDBC" />
            <datasource type="POOLED">
                <property name="driver" value="..." />
                <property name="url" value="..." />
                <property name="username" value="..." />
                <property name="password" value="..." />
        </environment>
    <environments>
</configuration>

Upvotes: 1

Views: 6130

Answers (1)

If you are using mybatis-spring-boot-started you don't need to use mybatis.xml to specify datasource parameters. It is not used for database accesses initiated from spring.

In order to add connection pooling you need to add a dependency to any of the connection pool supported by spring-boot. The easiest way it do add a dependency to org.springframework.boot:spring-boot-starter-jdbc. If you have it the connections are already pooled.

Upvotes: 2

Related Questions