Is there really no official fix for the mybatis-spring multidatabase bug?

67 views
Skip to first unread message

dq

unread,
Nov 5, 2012, 5:02:36 PM11/5/12
to mybati...@googlegroups.com
I encountered the same problem as these people at the bottom of the page: http://code.google.com/p/mybatis/wiki/Spring when I tried to configure multiple databases with mybatis 3.1.1 and mybatis-spring 1.1.1.

I ultimately had to fix the problem myself by removing the final from SqlSessionDaoSupport's methods setSqlSessionFactory and setSqlSessionTemplate so that I can override them in my DAO classes and provide qualifiers to the autowiring so I can specify which database they're for.

Is there an official fix for this, or is it still officially broken?  Also, is this the right place for this, or is there a separate group for it?

Eduardo Macarron

unread,
Nov 6, 2012, 4:46:11 AM11/6/12
to mybati...@googlegroups.com
Sorry I did not read that wiki comments.

What is the problem?

dq

unread,
Nov 6, 2012, 9:35:26 AM11/6/12
to mybati...@googlegroups.com
My class extends from MapperFactoryBean, which extends from  SqlSessionDaoSupport.  All 3 classes are instantiated by Spring through their Autowiring.  The problem is that SqlSessionDaoSupport contains these two methods:  

@Autowired(required = false)
  public final void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
    if (!this.externalSqlSession) {
      this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
    }
  }

  @Autowired(required = false)
  public final void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
    this.sqlSession = sqlSessionTemplate;
    this.externalSqlSession = true;
  }

These methods take a SqlSessionFactory and a SqlSessionTemplate, both of which are tied to a particular database configuration.  If you have more than 1 database configured in your application, the Autowiring fails because it can't figure out which SqlSessionFactory and SqlSessionTemplate definition to use.  Since these methods don't take qualifiers and they're final, there is no way to get around this.

I removed the finals, compiled a new spring-mybatis jar, and override these methods in my own classes which do take qualifiers to identify which database configuration to use.  This works for me, but I don't think there's a fix out there in the wild.

Dridi Boukelmoune

unread,
Nov 6, 2012, 9:46:09 AM11/6/12
to mybati...@googlegroups.com
Hi,

Have you tried to override the bean definition ?
http://stackoverflow.com/a/4943673

Dridi
--
Dridi Boukelmoune
Développeur/Formateur

GSM : +33 (0)6 17 91 14 23

dq

unread,
Nov 6, 2012, 9:57:15 AM11/6/12
to mybati...@googlegroups.com
In that example the Autowired methods have Qualifiers, so the example still won't work with myBatis-Spring without eliminating the final declarations on the two methods in SqlSessionDaoSupport because they don't use any Qualifiers.  

This example is basically what I did, but in order to do it I had to override the SqlSessionDaoSupport methods in order to do it.

Eduardo Macarron

unread,
Nov 6, 2012, 11:01:07 AM11/6/12
to mybati...@googlegroups.com
Ok. It is fixed in r5479.

2012/11/6 dq <d...@kayak.com>:
Reply all
Reply to author
Forward
0 new messages