ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Lamey <cla...@localmatters.com>
Subject RE: Transaction issue with 2.2.0
Date Tue, 27 Feb 2007 20:00:56 GMT
If you're using the same datasource, you can easily wrap that in Spring
data source transaction manager and then use declarative transactions so
that your DAOs transparently join in existing transactions or create new
ones on demand:

Here's some sample Spring xml from a project I'm working on now:

<!-- setup datasource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
			destroy-method="close">
    <property
name="driverClassName"><value>org.postgresql.Driver</value></property>
    <property
name="url"><value>jdbc:postgresql://localhost/hydra</value></property>
    <property name="username"><value>ibatis</value></property>
    <property name="password"><value>ibatisroolz</value></property>
    <property
name="poolPreparedStatements"><value>true</value></property>
</bean>

<!-- setup the sqlmapclient -->
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation"
value="classpath:sqlmap-config.xml"/>
     <property name="dataSource" ref="dataSource"/>
</bean>

<!-- DAOs (generated by Abator using the generic SI methods) -->
<bean id="keywordDAO" class="com.localmatters.hydra.dao.KeywordDAOImpl">
    <property name="sqlMapClient" ref="sqlMapClient"/>
</bean>

<bean id="headingDAO" class="com.localmatters.hydra.dao.HeadingDAOImpl">
    <property name="sqlMapClient" ref="sqlMapClient"/>
</bean>

<!-- transactions -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource"><ref bean="dataSource"/></property>
</bean>
	
<bean id="transactionAttributes" 

class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
    <property name="properties">
        <value>
        update*=PROPAGATION_REQUIRED
        insert*=PROPAGATION_REQUIRED
        </value>
    </property>
</bean>
	
<bean id="matchAllTxInterceptor"

class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager"/>
    <property name="transactionAttributeSource"
ref="transactionAttributes"/>
</bean>

<bean id="autoProxyCreator" 

class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <property name="interceptorNames">
    <list>
        <idref local="matchAllTxInterceptor"/> 
    </list>
    </property>
    <property name="beanNames">
    <list>
        <idref local="keywordDAO"/>
        <idref local="headingDAO"/>
    </list>
    </property>
</bean>

In your case you'd want to define the class that's calling the DAOs in
Spring and make sure it and its methods are referenced in the
thetransactionAttributes and autoProxyCreator beans so they'd join in
the transaction fun.

But then you can grab the objects from Spring like this:

ClassPathXmlApplicationContext context 
			= new ClassPathXmlApplicationContext("ibatis-demo-context.xml");

KeywordService keywordService 
			= (KeywordDAO) context.getBean("keywordDAO");

And your code doesn't have to explicitly handle transactions, it's all
done for you based on class a method naming.

Cheers,
Chris

On Tue, 2007-02-27 at 12:33 -0700, Dave Rodenbaugh wrote:
> Well, the answer is that we're code generating these two separate
> modules...they have code-generation time differences, but at runtime,
> we're sharing the same DataSource for both.  Part of the reason is
> modularity of the two, part of the reason is that I'm not in control
> of the database design so I'm stuck with it.
>  
> Is there a non-Spring solution that I can use?  I'm not anxious to
> introduce another set of libraries that are even more heavy weight
> than iBATIS, which is lovely.
>  
> -D
> 
> 
> ______________________________________________________________________
> From: Armeanu, Christian [mailto:ArmeanuC@hsd.k12.or.us] 
> Sent: Tuesday, February 27, 2007 12:27 PM
> To: user-java@ibatis.apache.org
> Subject: RE: Transaction issue with 2.2.0
> 
> 
> 
> That is correct.  Why are you using two different database resources
> to access the same resource anyway?
> 
>  
> 
> Chris
> 
>  
> 
>  
> 
>                                    
> ______________________________________________________________________
> From: Martin Bengl [mailto:martin@dailyspring.net] 
> Sent: Tuesday, February 27, 2007 11:22 AM
> To: user-java@ibatis.apache.org
> Subject: Re: Transaction issue with 2.2.0
> 
> 
>  
> 
> yes, thats correct - you should use the spring transactional support.
> If you are using only one database resource (only one oracle database
> for example) you needn't XA. you can use local jdbc transaction with
> spring.
> 
> see
> http://static.springframework.org/spring/docs/2.0.x/reference/transaction.html for more
details
> 
> greetings
> martin
> 
> Armeanu, Christian wrotes: 
> 
> I don’t know about the transaction support with iBATIS DAO, but if you
> could the recommended Spring DAO for example.  All you would need to
> do is to configure the appropriate transaction manager.  
> 
>  
> 
> Chris
> 
>  
> 
>                                    
> ______________________________________________________________________
> From: Dave Rodenbaugh [mailto:drodenbaugh@WILDBLUECORP.COM] 
> Sent: Tuesday, February 27, 2007 10:52 AM
> To: user-java@ibatis.apache.org
> Subject: RE: Transaction issue with 2.2.0
> 
> 
>  
> 
> If this is the answer, how does iBATIS support XA transactions?  Do I
> need to use a different Oracle library, or configure this differently?
> I'm not an XA expert...
> 
>  
> 
> Thanks,
> 
> -D
> 
>  
> 
>                                    
> ______________________________________________________________________
> From: Armeanu, Christian [mailto:ArmeanuC@hsd.k12.or.us] 
> Sent: Tuesday, February 27, 2007 10:12 AM
> To: user-java@ibatis.apache.org
> Subject: RE: Transaction issue with 2.2.0
> 
> For all I know you need XA transactions for this to work…
> 
>  
> 
> Chris
> 
>  
> 
>                                    
> ______________________________________________________________________
> From: Dave Rodenbaugh [mailto:drodenbaugh@WILDBLUECORP.COM] 
> Sent: Tuesday, February 27, 2007 9:07 AM
> To: user-java@ibatis.apache.org
> Subject: RE: Transaction issue with 2.2.0
> 
> 
>  
> 
> Oh...one other piece of possibly relevant info:
> 
>  
> 
> FooDao and BarDao are created in two separate JARs, each having their
> own configuration properties (they point to the same DB, but different
> schemas.  They share the same runtime user information).
> 
>  
> 
> 
> Thanks,
> 
> 
> -D
> 
> 
>                                    
> ______________________________________________________________________
> From: Dave Rodenbaugh 
> Sent: Tuesday, February 27, 2007 10:05 AM
> To: 'user-java@ibatis.apache.org'
> Subject: Transaction issue with 2.2.0
> 
> Hello,
> 
> 
>  
> 
> 
> I have a question about a transaction scenario I'm doing...I see
> problems when trying to use
> startTransaction()/commitTransaction()/endTransaction with JDBC data
> source.  I'm using Oracle 9.2 client libraries, and this is with
> iBATIS-2.2.0.
> 
> 
>  
> 
> 
> Here's my code pattern--I have two Daos involved--one "Foo" and one
> "Bar".  I'm transacting Foo here.  (I've tried transacting both and
> get the same result, too--so it seems to be relating to the FooDao
> transaction somehow)
> 
> 
>  
> 
> 
> DaoManager dm = FooDaoManager.getDaoManager();
> 
> 
> try
> 
> 
> {
> 
> 
>     dm.startTransaction();
> 
> 
>     op1UsingFooDao();
> 
> 
>     logger.info("1");
> 
> 
>     op2UsingFooDao();
> 
> 
>     logger.info("2");
> 
> 
>     op3UsingBarDao();                  //throws SQLException when the
> Tx code is off, hangs here when using Tx
> 
> 
>     logger.info("3");    //never gets here...
> 
> 
>     dm.commitTransaction();
> 
> 
> }
> 
> 
> finally
> 
> 
> {
> 
> 
>     logger.info("Inside finally block");
> 
> 
>     dm.endTransaction();
> 
> 
> }
> 
> 
>  
> 
> 
> Output with no transaction code (application terminates):
> 
> 
> 1
> 
> 
> 2
> 
> 
> Inside finally block
> 
> 
>  
> 
> 
> Output with transaction code present (application hangs):
> 
> 
> 1
> 
> 
> 2
> 
> 
>  
> 
> 
> I expected that the transaction would roll back when an exception was
> encountered by triggering the finally code.  However, since it hangs,
> I don't know where or why it hung in op3UsingBarDao()...?
> 
> 
>  
> 
> 
> What am I doing wrong in this scenario and how should I correctly
> address this?  This scenario is consistent with the documentation, but
> the docs don't cover multiple DaoManager transactions...
> 
> 
>  
> 
> 
> Thanks,
> 
> 
> -Dave
> 
> 
>  
> 
> 

Mime
View raw message