In condition of shardingjdbc, the 'autocommit' in the datasource configuration does not take effect
i use sharding jdbc+mybatis+spring
sharding two db, one master two slave 。
spring config:
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="shardingDataSource"/>
</bean>
<!-- 使用annotation注解方式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- mybatis 配置 sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="shardingDataSource"/>
<property name="configLocation" value="classpath:mybatis/config/mybatis-config.xml"></property>
<property name="mapperLocations"
value="classpath:mybatis/mapper/mysql/**/*Mapper.xml">
</property>
</bean>
<!-- mybatis 配置 SQLSession模板 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"
scope="prototype">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!-- mybatis 配置 自动生成Dao接口实现 -->
<bean id="mybatisMapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"
value="**.com.xx.xxxx.xxxx.xxxxxx.**.dao.**"/>
<!-- <property name="basePackage"
value="com.xx.xxxx.xxxx.xxxxxx.dao"/> -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--spring 的JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="shardingDataSource">
</property>
</bean>
connect pool autoCommit is configed 'false' <mvn.dbcp.defaultAutoCommit>false</mvn.dbcp.defaultAutoCommit>
java code in manager layer:
i wrote ‘@Transactional’ out
// @Transactional
public Integer batchUpdateByDetailList(List
sql mapper: < update id="updateByMd5Value" parameterType="com.xx.xxxx.xxxx.xxxxxx.dto.MaintainDetailDto" > update maintain_detail < trim prefix="set" suffixOverrides="," > qty = #{qty,jdbcType=DECIMAL}, </ trim > WHERE is_delete = 0 and md5_value = #{md5Value,jdbcType=VARCHAR} and tenant_id = #{tenantId,jdbcType=VARCHAR} </ update >
test code:
@Test
public void batchUpdateByDetailListTest() {
List
result: response success:Updates: 1 But table‘s record do not update
i find sharding‘s class: AbstractConnectionAdapter, autoCommit is set to 'true' when sql do not managed by spring transactions,spring may create new connection of shardingjdbc,autoCommit is set to 'true', when mybatis commit, find autoCommit is true, do not commit sql. the SQL is routed to a single database for execution sharding sphere vesion : 4.1.1 DataSource : dbcp2
so ,What should I do with this non spring transaction managed of update or insert?
@cherrylzhao This issue has a clear description. Could you give it a look?
ShardingSphere don't care about the status of underlying datasource pool, <mvn.dbcp.defaultAutoCommit>false</mvn.dbcp.defaultAutoCommit> will not effective.
I think you can get the connection from jdbcTemplate, then do the transaction control manually like that
connection.setAutoCommit(false)
....
connection.commit()
ShardingSphereConnection should be managing its own logic tx-auto-commit status, avoid to use underlying physical connnections. Consider about two underlying connnections, one with auto-commit, one with not.
@fantacy2001, Hi, can you summary the title for search friendly?
@fantacy2001, Hi, can you summary the title for search friendly?
OK,has Modified
It is recommended not to configure ‘autocommit‘’ in datasource properties