2015-09-06 106 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,則只需執行一次)。分攤連接創建使用連接池。 –

相關問題