Lazy Database Connection

61 views
Skip to first unread message

Ryan Shelley

unread,
May 24, 2012, 4:48:40 PM5/24/12
to mybatis-user
We started implementing our app with straight JDBC, but then added
MyBatis, and when we did that, we noticed that MyBatis initializes a
connection to the database on startup (we're using Spring).

Our unit tests don't use the actual database (our integration tests
do), so when we run our unit tests MyBatis attempts to connect to the
database (which the developer might not have since they are working
with mocks). So even though no queries are executing during unit test
phase, a DB connection is still attempting to be established.

We've worked around it temporarily by only instantiating MyBatis
classes from the integration test JUnit config, but it means that
integrators to our service need to import two context files (one for
the application, and one for the MyBatis beans), which we're not a fan
of. It'd be nice to only make those connections to the database when
we first query the database.

Is there a way to lazy-load the database connections so it doesn't
happen as soon as MyBatis beans are fired up? Or maybe this is a
question for the MyBatis-spring mailing list...

Thanks!

-Ryan

Eduardo Macarron

unread,
May 24, 2012, 4:59:04 PM5/24/12
to mybati...@googlegroups.com
Hi Ryan. MyBatis 3.1 does in fact open a connection when it is needed
but Springs gets one when a transaction is started, This will also
happen when using a JdbcTemplate so maybe the question would better go
in the spring forum.

2012/5/24 Ryan Shelley <rfshe...@gmail.com>:

Ryan Shelley

unread,
May 24, 2012, 5:01:02 PM5/24/12
to mybatis-user
Excellent. Thanks for the prompt response!

-Ryan

On May 24, 1:59 pm, Eduardo Macarron <eduardo.macar...@gmail.com>
wrote:
> Hi Ryan. MyBatis 3.1 does in fact open a connection when it is needed
> but Springs gets one when a transaction is started, This will also
> happen when using a JdbcTemplate so maybe the question would better go
> in the spring forum.
>
> 2012/5/24 Ryan Shelley <rfshelle...@gmail.com>:

Ryan Shelley

unread,
May 24, 2012, 5:02:27 PM5/24/12
to mybatis-user
I just realized that MyBatis-spring uses this same mailing list...

Eduardo Macarron

unread,
May 25, 2012, 1:45:18 AM5/25/12
to mybati...@googlegroups.com
Hi, yes and sorry if I was not clrear. MyBatis-Spring forum is also
this but I was referring to the spring forum.

http://forum.springsource.org/

Do a fast test with JdbcTemplate to make sure it behaves the same and
post there. You will have more chances that your post is replied if it
is about JdbcTemplate than about MyBatis (not because they do not love
MyBatis, cos they do ;) but because there are more people using it.

Let us know!

2012/5/24 Ryan Shelley <rfshe...@gmail.com>:

Kurth, Felix

unread,
May 25, 2012, 2:49:49 AM5/25/12
to mybati...@googlegroups.com
You can achieve this by proxying your datasource bean:

<bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource" ref="originalDataSource" />
</bean>
Reply all
Reply to author
Forward
0 new messages