日期:2014-05-19  浏览次数:20790 次

spring结合mybatis多数据源
spring 中以配置多个数据源,利用mybatis实现sql语句
现在取出一个数据库,执行sql语句没有问题,但取出第二个时,不能执行sql语句了,提示java.sql.Connection - xxx Connection Closed怎么办


我的配置文件如下:
 <bean id="ds1" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
  <property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
  </property>
  <property name="url">
<value>jdbc:MYSQL://127.0.0.1:3306/MYGPS_SY?characterEncoding=utf8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>

<bean id="ds2" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
  <property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:MYSQL://127.0.0.1:3306/MYGPS_AS?characterEncoding=utf8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
 
<!-- 数据管理 -->
<bean id="transactionmanager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds1"/>
</bean>
<bean id="s1" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="mapperLocations" value="classpath:gpsData/transform/dao/GpsDao.xml" ></property>
<property name="dataSource" ref="ds1"/>
</bean>
<bean id="gpsdao1" class="org.mybatis.spring.MapperFactoryBean">
<property name="mapperInterface" value="gpsData.transform.dao.GpsDao"/>
<property name="SqlSessionFactory" ref="s1"/>
</bean>

<bean id="transactionmanager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds2"/>
</bean>

<bean id="sqlsessionfactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="mapperLocations" value="classpath:gpsData/transform/dao/GpsDao.xml" ></property>
<property name="dataSource" ref="ds2"/>
</bean>
 <bean id="gpsdao2" class="org.mybatis.spring.MapperFactoryBean">
<property name="mapperInterface" value="gpsData.transform.dao.GpsDao"/>
<property name="SqlSessionFactory" ref="sqlsessionfactory2"/>
</bean>

GpsDao gpsdao1=(GpsDao) context.getBean("gpsdao1");
GpsDao gpsdao2 =(GpsDao) context.getBean("gpsdao2");
Target target2=(Target)gpsdao1.getTarget();
gpsdao2.insertTarget(target2);

执行到insertTarget时就不行了
这是mybatis的mapper配置文件
<select id="getTarget" parameterType="java.lang.String" resultType="gpsData.transform.action.Target">
SELECT T_TargetID as t_targetID,T_ParentID as t_parentID FROM t_target
</select>
<insert id="insertTarget" parameterType="gpsData.transform.action.Target" >
insert into t_target