You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by lsalotto <ls...@gmail.com> on 2015/04/24 22:34:52 UTC

Re: blueprint + myBatis + transactions

I came across this post when trying to accomplish same task. I have just
(minutes ago) succesfully managed to get myBatis working transactionally
with an OSGi blueprint configuration.
The problem with documentation is that it is based upon Spring
configuration, not OSGi blueprint, so it's bit more tricky to get it
working. It would be good if Claus can get it updated or at least point to
this thread if you consider this to be a proper solution

I had to digg a lot to get this working, but mostly based on this post from
SO http://stackoverflow.com/questions/25045264/camel-mybatis-apache-aries

I'm using Red Hat JBoss Fuse 6.1, so it should work for your case too. Here
are my configuration files. I have a beans.xml where I config all OSGi
beans, and then I got a blueprint.xml where I keep camel routes, then of
course I have myBatis SQLMapConfig.xml and mapping files.


Clause/Willem, let me know if you think this is a good approach or if
there's something that should be accomplished in another way.




*beans.xml*

> <?xml version="1.0" encoding="UTF-8"?>
> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
> 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:camel="http://camel.apache.org/schema/blueprint"
> 	xmlns:cxf="http://cxf.apache.org/blueprint/core"
> xmlns:cxfrs="http://camel.apache.org/schema/blueprint/cxf"
> 	xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs"
> 	xsi:schemaLocation="
>        http://www.osgi.org/xmlns/blueprint/v1.0.0
> http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
>        http://camel.apache.org/schema/blueprint
> http://camel.apache.org/schema/blueprint/camel-blueprint.xsd
>        http://cxf.apache.org/blueprint/jaxrs
> http://cxf.apache.org/schemas/blueprint/jaxrs.xsd 
>        http://cxf.apache.org/blueprint/core
> http://cxf.apache.org/schemas/blueprint/core.xsd 
>        http://camel.apache.org/schema/blueprint/cxf
> http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd">
>            

> 
> 	
> <bean id="ds" class="org.apache.commons.dbcp2.BasicDataSource">
> 		
> <property name="driverClassName"
> value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
> 		
> <property name="url"
> value="jdbc:sqlserver://hostname:1433;DatabaseName=DBNAME"/>
> 		
> <property name="username" value="user"/>
> 		
> <property name="password" value="password" />
> 	
> </bean>
> 		
> 	
> <bean id="dataSource"
> class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
> 	    
> <argument ref="ds" />
> 	
> </bean>
> 	
> 	
> <bean id="txManager"
> class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
>    		
> <property name="dataSource" ref="dataSource"/>
> 	
> </bean>
> 	
> 	
> <bean id="mybatisConfig"
> class="org.springframework.core.io.ClassPathResource">
> 	    
> <argument value="SqlMapConfig.xml" />
> 	
> </bean>
> 	
> 	
> <bean id="sqlSessionFactoryBean"
> class="org.mybatis.spring.SqlSessionFactoryBean">
> 	    
> <property name="dataSource" ref="dataSource"/>
> 	    

> 		
> <property name="configLocation" ref="mybatisConfig"/>
> 	    

> 		
> <property name="mapperLocations">
> 	        
> <array value-type="org.springframework.core.io.Resource">
> 		            
> <bean class="org.springframework.core.io.ClassPathResource">
> 		                
> <argument value="Paciente.xml" />
> 		            
> </bean>
> 	        
> </array>
> 	    
> </property>
> 	
> </bean>
> 	
> 	
> <bean id="sqlSessionFactory"
> class="org.apache.ibatis.session.SqlSessionFactory"
> factory-ref="sqlSessionFactoryBean" factory-method="getObject" />
> 	
> 	
> <bean id="mybatis"
> class="org.apache.camel.component.mybatis.MyBatisComponent">
>     	
> <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
> 	
> </bean>
> 	
> <bean id="PROPAGATION_REQUIRED"
> class="org.apache.camel.spring.spi.SpringTransactionPolicy">
> 	    
> <property name="transactionManager" ref="txManager"/>
> 	    
> <property name="propagationBehaviorName" value="PROPAGATION_REQUIRED"/>
> 	
> </bean>
> 
> </blueprint>


*blueprint.xml*

> <?xml version="1.0" encoding="UTF-8"?>
> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
> 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:camel="http://camel.apache.org/schema/blueprint"
> 	xmlns:cxf="http://cxf.apache.org/blueprint/core"
> xmlns:cxfrs="http://camel.apache.org/schema/blueprint/cxf"
> 	xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs"
> 	xsi:schemaLocation="
>        http://www.osgi.org/xmlns/blueprint/v1.0.0
> http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
>        http://camel.apache.org/schema/blueprint
> http://camel.apache.org/schema/blueprint/camel-blueprint.xsd
>        http://cxf.apache.org/blueprint/jaxrs
> http://cxf.apache.org/schemas/blueprint/jaxrs.xsd 
>        http://cxf.apache.org/blueprint/core
> http://cxf.apache.org/schemas/blueprint/core.xsd 
>        http://camel.apache.org/schema/blueprint/cxf
> http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd">
> 	
> <camelContext trace="false" id="blueprintContext"                                                                                                                                         
> xmlns="http://camel.apache.org/schema/blueprint">
> 		
> 		
> <route autoStartup="true" id="atransactedMyBatisRoute">
> 			

>                         
> <from uri="cxfrs:bean:acxfEndopoint?bindingStyle=SimpleConsumer" />
> 			
> <to uri="someProcessor" />
> 			
> <transacted />
> 			
> <to uri="mybatis:firstMyBatisEndpoint?statementType=SelectOne"/>
> 			
> <to uri="otherProcessor" />
> 			
> <to uri="mybatis:secondMyBatisEndpoint?statementType=SelectOne" />
> 	
> 			
> <split>
> 				
> <simple>
> ${header.someHeader}
> </simple>
> 				
> <to uri="anotherProcessor" />
> 				
> <to uri="mybatis:thirdMyBatisEndpoint?statementType=SelectOne" />
> 			
> </split>
> 			
> <to uri="osPacienteASISGHProcessor" />
> 			
> <to uri="mybatis:actualizaObraSocPaciente?statementType=SelectOne" />
> 	
> 		
> </route>
> 	
> </camelContext>
> </blueprint>

*SQLMapConfig.xml*: Please note that you MUST NOT define datasources or
mappers here, will cause some duplicate mapping issues (it did that for me
till I realized I still had the mapping files defined in this file, AND ALSO
defined in beans.xml)

> &lt;
> !DOCTYPE configuration
>     PUBLIC &quot;-//mybatis.org//DTD Config 3.0//EN&quot;
>     &quot;http://mybatis.org/dtd/mybatis-3-config.dtd&quot;
> &gt;
>  
> <configuration>
>  
>   
> <settings>
>     
> <setting name="useGeneratedKeys" value="false"/>
>   
> </settings>
>  
>   

>   
> <typeAliases>
>     
> <typeAlias alias="Paciente" type="org.pami.integrator.model.Paciente"/>
>   
> </typeAliases>
> 
> </configuration>

*MappingFile.xml*
a simple mapping file, nothing special here


Hope this helps!!

Luciano Salotto



--
View this message in context: http://camel.465427.n5.nabble.com/blueprint-myBatis-transactions-tp5764629p5766311.html
Sent from the Camel - Users mailing list archive at Nabble.com.