<?xml version="1.0" encoding="UTF-8" ?>
<!--

    Copyright ${license.git.copyrightYears} the original author or authors.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:jdbc="http://www.springframework.org/schema/jdbc"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
     default-autowire="byName">

	<bean id="userTransactionService" class="com.atomikos.icatch.config.UserTransactionServiceImp"
		init-method="init" destroy-method="shutdownForce">
		<constructor-arg>
			<!-- Seems these settings make no difference -->
			<props>
				<prop key="com.atomikos.icatch.threaded_2pc">
					true
				</prop>
				<prop key="com.atomikos.icatch.serial_jta_transactions">
					false
				</prop>
			</props>
		</constructor-arg>
	</bean>

	<!-- Construct Atomikos UserTransactionManager, needed to configure Spring -->
	<bean id="transactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
		init-method="init" destroy-method="close" depends-on="userTransactionService">

		<!-- IMPORTANT: disable startup because the userTransactionService above 
			does this -->
		<property name="startupTransactionService" value="false" />

		<!-- when close is called, should we force transactions to terminate or 
			not? -->
		<property name="forceShutdown" value="false" />
	</bean>

	<!-- Also use Atomikos UserTransactionImp, needed to configure Spring -->
	<bean id="userTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"
		depends-on="userTransactionService">
		<property name="transactionTimeout" value="300" />
	</bean>

	<!-- Configure the Spring framework to use JTA transactions from Atomikos -->
	<bean id="JtaTransactionManager"
		class="org.springframework.transaction.jta.JtaTransactionManager"
		depends-on="userTransactionService">
		<property name="transactionManager" ref="transactionManager" />
		<property name="userTransaction" ref="userTransaction" />
	</bean>

	<tx:annotation-driven transaction-manager="JtaTransactionManager" />

	<context:component-scan base-package="org.mybatis.spring.submitted.xa" />

	<bean id="master" class="com.atomikos.jdbc.AtomikosDataSourceBean"
		init-method="init" destroy-method="close">
		<property name="uniqueResourceName" value="masterDBUniqueName" />
		<property name="xaDataSourceClassName" value="org.apache.derby.jdbc.EmbeddedXADataSource" />
		<property name="minPoolSize">
			<value>1</value>
		</property>
		<property name="maxPoolSize">
			<value>5</value>
		</property>
		<property name="xaProperties">
			<props>
				<prop key="databaseName">memory:masterDB</prop>
				<prop key="createDatabase">create</prop>
			</props>
		</property>
	</bean>

	<bean id="slave" class="com.atomikos.jdbc.AtomikosDataSourceBean"
		init-method="init" destroy-method="close">
		<property name="uniqueResourceName" value="slaveDBUniqueName" />
		<property name="xaDataSourceClassName" value="org.apache.derby.jdbc.EmbeddedXADataSource" />
		<property name="minPoolSize">
			<value>1</value>
		</property>
		<property name="maxPoolSize">
			<value>5</value>
		</property>
		<property name="xaProperties">
			<props>
				<prop key="databaseName">memory:slaveDB</prop>
				<prop key="createDatabase">create</prop>
			</props>
		</property>
	</bean>

	<jdbc:initialize-database data-source="master">
		<jdbc:script
			location="classpath:org/mybatis/spring/submitted/xa/database-schema.sql" />
	</jdbc:initialize-database>

	<jdbc:initialize-database data-source="slave">
		<jdbc:script
			location="classpath:org/mybatis/spring/submitted/xa/database-schema.sql" />
	</jdbc:initialize-database>

	<bean id="sqlSessionFactoryMaster" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="databaseIdProvider">
			<null />
		</property>
		<property name="dataSource" ref="master" />
		<property name="typeAliasesPackage" value="org.mybatis.jpetstore.domain" />
	</bean>

	<bean id="userMapperMaster" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="sqlSessionFactory" ref="sqlSessionFactoryMaster" />
		<property name="mapperInterface" value="org.mybatis.spring.submitted.xa.UserMapper" />
	</bean>

	<bean id="sqlSessionFactorySlave" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="databaseIdProvider">
			<null />
		</property>
		<property name="dataSource" ref="slave" />
		<property name="typeAliasesPackage" value="org.mybatis.jpetstore.domain" />
	</bean>

	<bean id="userMapperSlave" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="sqlSessionFactory" ref="sqlSessionFactorySlave" />
		<property name="mapperInterface" value="org.mybatis.spring.submitted.xa.UserMapper" />
	</bean>

</beans>	
