Skip to content

mybatis使用sjdbc做读写分离时报错(仅仅做读写分离,不做分片) #362

Closed
@wangqingchengsg

Description

使用 1.5.3版本

预期行为:插入无报错

实际行为:插入报错

步骤:

1.报错时的配置:

  <bean id="ds_0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/ds_0"/>
    <property name="username" value="root"/>
    <property name="password" value="wqch"/>
</bean>

<bean id="ds_1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/ds_1"/>
    <property name="username" value="root"/>
    <property name="password" value="wqch"/>
</bean>

<rdb:master-slave-data-source id="shardingDataSource" master-data-source-ref="ds_0" slave-data-sources-ref="ds_1" />

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="shardingDataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

2.具体的错误:
Exception in thread "main" org.springframework.jdbc.BadSqlGrammarException:

Error updating database. Cause: java.sql.SQLException: No value specified for parameter 1

The error may involve defaultParameterMap

The error occurred while setting parameters

SQL: INSERT INTO t_order(order_id,user_id, status) VALUES ( ?, ?, ? )

Cause: java.sql.SQLException: No value specified for parameter 1

; bad SQL grammar []; nested exception is java.sql.SQLException: No value specified for parameter 1

注:但是在执行例如查询所有,删除所有等没有传递参数的操作,程序将顺利执行

3注假如不使用读写分离,仅仅注释一下配置,不用改代码,程序将顺利执行插入操作

4.github地址 https://github.com/wangqingchengsg/sharding-jdbc-example-mybatis

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions