emacarron / mybatis

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

Annotation-based and AOP-based sessions #190

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What version of the MyBatis are you using?
mybatis-3.0.2.jar
mybatis-spring-1.0.0-RC1.jar

Please describe the problem.  Unit tests are best!
SqlSessionTemplate.execute allows to enclose several sentences in one session 
with one ExecutorType, but requires procedural code. It will be nice to add 
support for annotation-based and AOP-based template execution. 

What is the expected output? What do you see instead?
New feature

Please provide any additional information below.

Example of annotation-based specification:
- Declaring <bean class="org.mybatis.spring.AnnotationConfig"> enables 
annotation-based sessions.
- The annotation @InSqlSession in a bean method executes the method inside a 
session of the default SqlSessionFactory with default ExecutorType.
- @InSqlSession("factory") will do it with specific SqlSessionFactory.
- @InSqlSession(type=ExecutorType.BATCH) will do ir with specific type.
- @InSqlSession(factory="factory", type=ExecutorType.BATCH) will also work.
- @InSqlSession(factories={"factory1", "factory2"}, types={ExecutorType.BATCH, 
ExecutorType.SIMPLE}) will enclose inside two sessions of two different 
factories. And so on.

Example of AOP-based specification:

  <bean id="sqlSessionAspect" class="org.mybatis.spring.SqlSessionAspect">
  </bean>
  <aop:config>
    <aop:aspect ref="respuestaAspect">
      <aop:pointcut id="myPointcut" expression="execution(* my.package.*.*(..))" />
      <aop:around pointcut-ref="sqlSessionAspect" method="execute"/>
    </aop:aspect>
  </aop:config>

Original issue reported on code.google.com by boccaro...@gmail.com on 1 Dec 2010 at 11:55

GoogleCodeExporter commented 9 years ago
I forget ExecutorType for AOP:

<bean id="batchSqlSessionAspect" class="org.mybatis.spring.SqlSessionAspect">
  <property name="type" value="BATCH"/>
</bean>

Original comment by boccaro...@gmail.com on 1 Dec 2010 at 11:58

GoogleCodeExporter commented 9 years ago
Hi boccaromel. That has changed a bit from RC1 to RC3. Now the callback is 
gone, but the possibilities are almost the same.

I am not sure that a SqlSessionFactory should be selected by an annotation. I 
would say that this is not a runtime parameter. I mean, your beans should just 
use a SqlSession, any dependency to SqlSessionFactory should be in the 
bootstrap code/xml context.

But I do agree that it would be great to be able to specify the ExecutorType as 
a property attached to the transaction. In fact it _is_ a SqlSesion property 
and thus a transaction property. 

The problem is that if you start a batch session in a bean and you reuse other 
bean that has a different template it will fail because the second one will 
attach to the runnning transaction but a check is made to detect that the 
executor type is not the same that the one that the transaction was started 
with, and as MyBatis does not let to change the Executor type, an exception 
will be thrown. 

The problem is that I have no idea of how to do this. Transactions can be 
demarcated with annotations or AOP, and we don´t start it, Spring does. When 
we create the session, we don't know where that transaction was started. 

This could be easy to do if Spring let users attach custom properties to 
transactions: like @Transactional(mycustomatribute) that could later on got 
from TransactionManager. But that is not the case. :(

I think this will very difficult to implement in a consistent way. I think this 
is more a Spring issue than a MyBatis-Spring one.

Original comment by eduardo.macarron on 3 Dec 2010 at 6:12

GoogleCodeExporter commented 9 years ago

Original comment by eduardo.macarron on 9 Dec 2010 at 4:20