gpt4 book ai didi

Spring Mybatis Tomcat - 不使用 Spring 的事务

转载 作者:行者123 更新时间:2023-11-28 22:03:30 28 4
gpt4 key购买 nike

Spring Transaction 不适用于使用 TOMCAT 的以下配置。

spring: version: 3.1.2.RELEASE
spring.ws:version:2.0.5.RELEASE
Mybatis: version:3.1.1
Database: Oracle 11g
Datasource connection:com.oracle.ojdbc6.11.2.0
Server : Tomcat : apache-tomcat-6.0.32

我们正在尝试使用 AOP 声明式事务实现来创建事务服务。该设置工作正常,在集成测试期间按预期创建事务,但在使用端点从 SoapUi 运行测试时,在本地服务器(Tomcat)上,所有事务都是非事务性的(可以在日志文件中看到相同的)

SPRING-DAOS.xml

 xml version="1.0" encoding="UTF-8"
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="mywebappActiveTransactionManager" />

<aop:aspectj-autoproxy/>

<!-- ensure that the above transactional advice runs for any execution
of an operation defined by the Service interface -->
<aop:config>
<aop:pointcut id="transactionalOperation" expression="execution(* com.org1.org2.mywebappgate.services.*.*(..)) "/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionalOperation"/>
</aop:config>

<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) -->
<tx:advice id="txAdvice" transaction-manager="mywebappActiveTransactionManager">
<tx:attributes>
<tx:method name="*" rollback-for="SQLException"/>
<!-- <tx:method name="*" read-only="false" propagation="REQUIRED" /> -->
<!-- <tx:method name="*" read-only="false" propagation="REQUIRED" /> -->
</tx:attributes>
</tx:advice>


<jee:jndi-lookup id="mywebappActiveDataSource" jndi-name="java:/comp/env/jdbc/mywebapp-active" />

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

<bean id="mywebappActiveSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="mywebappActiveDataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>

<bean id="mywebappActiveMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.org1.org2.mywebappgate.dao" />
<property name="sqlSessionFactoryBeanName" value="mywebappActiveSessionFactory" />
</bean>

Tomcat 上下文.xml

<Context>

<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>


<Resource auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver" maxActive="20" maxIdle="10" maxWait="-1" name="jdbc/mywebapp-active" password="xxxxxx" type="javax.sql.DataSource" url="jdbc:oracle:thin:@mywebappdc1.company:1521:mywebappdc1" username="xxxxxx"/>

mybatis-config.xml

<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- changes from the defaults -->
<setting name="lazyLoadingEnabled" value="false" />
</settings>

<mappers>
<mapper resource="company/mywebappgate/dao/SimpleCommentDao.xml" />
<mapper resource="company/mywebappgate/dao/CommentDao.xml"/>
<mapper resource="company/mywebappgate/dao/ARDAO.xml"/>
<mapper resource="company/mywebappgate/dao/ASIndicatorsDao.xml"/>
</mappers>

Junit 测试日志:

org.mybatis.spring.SqlSessionUtils: Creating a new SqlSession
org.mybatis.spring.SqlSessionUtils: Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c3c44]
org.mybatis.spring.transaction.SpringManagedTransaction: JDBC Connection [oracle.jdbc.driver.T4CConnection@1d5d765] will be managed by Spring
org.mybatis.spring.SqlSessionUtils: Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c3c44]
org.mybatis.spring.SqlSessionUtils: Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c3c44] from current transaction
org.mybatis.spring.SqlSessionUtils: Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c3c44]
org.mybatis.spring.SqlSessionUtils: Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c3c44]
org.springframework.context.support.GenericApplicationContext: Closing org.springframework.context.support.GenericApplicationContext@1b4fad5: startup date [Wed Sep 05 14:16:30 EDT 2012]; root of context hierarchy
org.springframework.beans.factory.support.DefaultListableBeanFactory: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@69d02b: defining beans [org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,transactionalOperation,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0,txAdvice

Tomcat 日志:

org.mybatis.spring.SqlSessionUtils: Creating a new SqlSession
org.mybatis.spring.SqlSessionUtils: SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1e55d39] was not registered for synchronization because synchronization is not active
org.mybatis.spring.transaction.SpringManagedTransaction: JDBC Connection [jdbc:oracle:thin:@mywebapp1.company:1521:mywebapp1, UserName=user, Oracle JDBC driver] will not be managed by Spring
org.mybatis.spring.SqlSessionUtils: Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1e55d39]
org.mybatis.spring.SqlSessionUtils: Creating a new SqlSession
org.mybatis.spring.SqlSessionUtils: SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5b02a6] was not registered for synchronization because synchronization is not active
org.mybatis.spring.transaction.SpringManagedTransaction: JDBC Connection [jdbc:oracle:thin:@mywebappdc1.company:1521:mywebappdc1, UserName=mywebappSOA01, Oracle JDBC driver] will not be managed by Spring

最佳答案

如果您使用的是 Spring,则无需在 mybatis-*.xml 中配置 xml。您只需要在 mywebappActiveSessionFactory 中包含一个属性:

<property name="mapperLocations" 
value="classpath*:company/mywebappgate/dao//*Mapper.xml"/>

你的 mybatis-config.xml 将是

<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- changes from the defaults -->
<setting name="lazyLoadingEnabled" value="false" />
</settings>

关于Spring Mybatis Tomcat - 不使用 Spring 的事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12302598/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com