mybatis / mybatis/spring

No scoped proxy for SqlSessionTemplate with Spring JavaConfig

Open
#116 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
2.9k
Forks
2.6k
Avg merge
3d 4m
Merged PRs (30d)
8

Description

I am trying to make Mappers serializable (kind of). To do so, I would simply want to wrap the SqlSessionTemplate (which is the non-serializable thing) via Java config into a scoped proxy...

@Configuration
@MapperScan(basePackages={"com.example.mybatis.mappers"}, sqlSessionTemplateRef="sqlSession")
public class SpringConfiguration {

    @Bean
    @Scope(proxyMode=ScopedProxyMode.TARGET_CLASS)
    public SqlSessionTemplate sqlSession(final SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

Unfortunately, this doesn't work, since the scoped proxy for the SqlSessionTemplateseems to be created, but what's injected into the Mapper in the end is still a standard SqlSessionTemplate (and not the Proxy around it), which has somehow escaped the proxy. I guess it gets set from some sqlSessionTemplate.getMapper call.

Edit: Yep, the problematic part seems to be this method from org.mybatis.spring.SqlSessionTemplate...

    @Override
    public <T> T getMapper(Class<T> type) {
        return getConfiguration().getMapper(type, this);
    }

Of course, thishere refers to the original SqlSessionTemplate object and not the Proxy around it.

Is there a way to force my scoped proxy into the mappers?

Of course, what works is to simply make the mappers themselves scoped proxies, but that seems to be less elegant and concise, so I would prefer making the problematic object a scoped proxy, since the MapperProxyitself is already Serializable and could be serialized - it just gets prevented by the SqlSessionTemplate.

(Of course, the non-existing default constructor will also be a problem...)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the SpringConfiguration example and inspect org.mybatis.spring.SqlSessionTemplate#getMapper, then trace how @MapperScan obtains the mapper and its SqlSession. Reproduce the scoped-proxy configuration and serialization behavior. Done means the mapper uses the configured scoped proxy rather than the original SqlSessionTemplate, or the limitation is documented with a supported alternative.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.