2015-09-06 107 views
0

在我的web应用程序中,我使用的是spring + spring-webmvc + mybatis,并且使用了jndi数据源。spring + mybatis,如何在http请求中共享数据库连接

我创建了一个mvc控制器来处理用户的登录请求。

在控制器中,我需要完成一些与数据库相关的任务,每个任务将访问一个服务对象,该对象具有Spring自动连线的mybatis映射器,这将创建一个mybatis sqlsession并使用它并关闭它。

我的问题是,我们可以使所有这些任务共享相同的mybatis sqlsession?

从我的理解,mybatis sqlsession意味着一个jdbc连接涉及。

我不想浪费任何资源。

编辑:

here is the logging message in the real example of my application: 
19:26:29.959 [http-8080-3] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession 
19:26:29.959 [http-8080-3] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [[email protected]] was not registered for synchronization because synchronization is not active 
19:26:30.001 [http-8080-3] DEBUG org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [jdbc:h2:tcp://127.0.0.1:6001/cmp, UserName=SA, H2 JDBC Driver] will not be managed by Spring 
19:26:30.002 [http-8080-3] DEBUG org.codingfarm.cwe.sys.mappers.UrlRoleMapper.countByExample - ==> Preparing: select count(*) from CMP.PUBLIC.SYS_URL_ROLES WHERE (URL = ?) 
19:26:30.002 [http-8080-3] DEBUG org.codingfarm.cwe.sys.mappers.UrlRoleMapper.countByExample - ==> Parameters: index.jsp(String) 
19:26:30.008 [http-8080-3] DEBUG org.codingfarm.cwe.sys.mappers.UrlRoleMapper.countByExample - <==  Total: 1 
19:26:30.008 [http-8080-3] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [[email protected]] 
19:26:30.008 [http-8080-3] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession 
19:26:30.009 [http-8080-3] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [[email protected]] was not registered for synchronization because synchronization is not active 
19:26:30.010 [http-8080-3] DEBUG org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [jdbc:h2:tcp://127.0.0.1:6001/cmp, UserName=SA, H2 JDBC Driver] will not be managed by Spring 
19:26:30.010 [http-8080-3] DEBUG org.codingfarm.cwe.sys.mappers.UrlRoleMapper.countByExample - ==> Preparing: select count(*) from CMP.PUBLIC.SYS_URL_ROLES WHERE (URL = ? and ROLE_ID in (? , ?)) 
19:26:30.011 [http-8080-3] DEBUG org.codingfarm.cwe.sys.mappers.UrlRoleMapper.countByExample - ==> Parameters: index.jsp(String), system(String), basic(String) 
19:26:30.012 [http-8080-3] DEBUG org.codingfarm.cwe.sys.mappers.UrlRoleMapper.countByExample - <==  Total: 1 
19:26:30.012 [http-8080-3] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [[email protected]] 

编辑: 根据的MyBatis-Spring文档:

SqlSessionTemplate是MyBatis的弹簧的心脏。它实现了SqlSession,并且是代替任何现有的SqlSession使用的替代品。 SqlSessionTemplate是线程安全的,可以被多个DAO或映射器共享。

那么如何让几个mappers共享一个SqlSessionTemplate?

+0

如果您将SqlSessionTemplate定义为singleton(这是默认值)bean,那么您的所有映射器应自动共享该一个实例。刚刚尝试通过注入各种映射器和是的,他们都有一个参考相同的SqlSessionTemplate。你是否可能将SqlSessionTemplate定义为Prototype? –

回答

0

最简单的方法是使用SqlSessionFactoryBean

如果你有这样的

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="configLocation" value="classpath:myBatisConfig.xml" /> 
</bean> 

的定义,然后,您可以通过一个

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
    <property name="mapperInterface" value="com.yourcompany.youapp.mapper.SomeMapper" /> 
    <property name="sqlSessionFactory" ref="sqlSessionFactory" /> 
</bean> 

定义映射豆一个或使用MapperScannerConfigurer通过扫描你的映射器类实例化所有这些

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
    <property name="basePackage" value="com.yourcompany.youapp.mapper" /> 
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 
</bean> 

然后mybatis会话将创建每春季交易a nd被所有实例化的映射器使用。

+0

使用mybatis:扫描功能也会自动注入工厂或模板,但是,我更喜欢明确定义映射器,工具。请参阅:https://mybatis.github.io/spring/mappers.html#scan –

+0

bu我正在寻找一种方法来为那些执行非事务操作的映射器共享mybatis会话。 – WestFarmer

+0

好的,这个问题不是很清楚。你真的需要这个吗? Mybatis SqlSession非常轻便。初始化mybatis配置并打开新的连接 - 这是需要时间的。配置创建应该完成一次(如果使用mybatis-spring,则只需执行一次)。分摊连接创建使用连接池。 –

相关问题