emacarron / mybatis

Automatically exported from code.google.com/p/mybatis
0 stars 0 forks source link

Setting a specific datasource for one mapper does not work #157

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What version of the MyBatis are you using?
MyBatis-Spring 1.0.0-RC1

Please describe the problem.  Unit tests are best!
If you set a datasource for a mapper and this mapper participates in the same 
transacction with other mapper that has a different datasource it will fail

What is the expected output? What do you see instead?
Wrong database access. I.e.:

org.springframework.jdbc.BadSqlGrammarException: SqlSession operation; bad SQL 
grammar []; nested exception is java.sql.SQLException: Table not found in 
statement [select * from accounts where id=?]
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:233)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
    at org.mybatis.spring.SqlSessionTemplate.wrapException(SqlSessionTemplate.java:251)
    at org.mybatis.spring.SqlSessionTemplate$7$1.doInSqlSession(SqlSessionTemplate.java:238)
    at org.mybatis.spring.SqlSessionTemplate.execute(SqlSessionTemplate.java:140)
    at org.mybatis.spring.SqlSessionTemplate.execute(SqlSessionTemplate.java:122)
    at org.mybatis.spring.SqlSessionTemplate$7.invoke(SqlSessionTemplate.java:233)
    at $Proxy11.getAccount(Unknown Source)
    at org.mybatis.spring.sample.datasource.FooServiceImpl.doSomeBusinessStuff(FooServiceImpl.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy13.doSomeBusinessStuff(Unknown Source)
    at org.mybatis.spring.sample.datasource.MyBatisDataSourceTest.testFooService(MyBatisDataSourceTest.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.sql.SQLException: Table not found in statement [select * from 
accounts where id=?]
    at org.hsqldb.jdbc.Util.throwError(Unknown Source)
    at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source)
    at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.instantiateStatement(PreparedStatementHandler.java:50)
    at org.apache.ibatis.executor.statement.BaseStatementHandler.prepare(BaseStatementHandler.java:64)
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.prepare(RoutingStatementHandler.java:39)
    at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:55)
    at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:40)
    at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:95)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:72)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:59)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:53)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:38)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:66)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:35)
    at $Proxy11.getAccount(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate$7$1.doInSqlSession(SqlSessionTemplate.java:236)
    ... 45 more

Please provide any additional information below.
Test attached

Original issue reported on code.google.com by eduardo.macarron on 23 Oct 2010 at 8:41

Attachments:

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
This problem may also happen when changing executor type. 

If this two sentences are executed in the same transaction: 
sqlSessionTemplate.execute(oneCallback, ExecutorType.SIMPLE); 
sqlSessionTemplate.execute(oneCallback, ExecutorType.BATCH); 
The second one will always be executed with the first executor type 
because it will reuse the SqlSession. 

Original comment by eduardo.macarron on 24 Oct 2010 at 11:35

GoogleCodeExporter commented 9 years ago
Fixed the ExecutorType issue. SqlSessionUtils now throws an exception if you 
try and change in the middle of a transaction.

For the original issue, we want to force users to use a second 
SqlSessionFactory for a second DataSource, correct? If that's the case, the 
easiest thing would be to override setDataSource() in SqlSessionTemplate and 
just throw OperationNotSupportedException. (Rather than copy/paste the 
JdbcAccessor code that deals with the ExceptionTranslator). 

Original comment by hpresnall@gmail.com on 24 Oct 2010 at 8:13

GoogleCodeExporter commented 9 years ago
fixed with:
- sqlSessionTemplate uses SqlSession ds not its parent's
- removed constructor with ds
- IllegalArgumentException if ds is attempted to be changed
- removed SqlSessionSupport datasource property
- removed MapperFactoryBean datasource property
- fixed tests 

Original comment by eduardo.macarron on 24 Oct 2010 at 9:18

Attachments:

GoogleCodeExporter commented 9 years ago

Original comment by eduardo.macarron on 1 Jan 2011 at 4:40