I am trying to test my transactions and Spring doesn't seem to be wrapping my method in a transaction.
Here is my applicationContext.xml:
My class with testFlightLeg method that should be wrapped in a transaction:Code:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:/hibernate.properties</value> </list> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url"><value>${hibernate.connection.url}</value> </property> <property name="driverClassName"><value>${hibernate.connection.driver_class}</value> </property> <property name="username"><value>${hibernate.connection.username}</value> </property> <property name="password"><value>${hibernate.connection.password}</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingResources"> <list> <value>my/path/UserTypes.hbm.xml</value> <value>my/path/Airport.hbm.xml</value> <value>my/path/AirSegment.hbm.xml</value> <value>my/path/Carrier.hbm.xml</value> <value>my/path/FlightLeg.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="airportDao" class="com.ngc.dts.domain.dao.hibernate.AirportHibernateDao"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="airSegmentDao" class="com.ngc.dts.domain.dao.hibernate.AirSegmentHibernateDao"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="carrierDao" class="com.ngc.dts.domain.dao.hibernate.CarrierHibernateDao"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="flightLegDao" class="com.ngc.dts.domain.dao.hibernate.FlightLegHibernateDao"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="flightService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="transactionManager"/> <property name="target" ref="flightServiceTarget"/> <property name="transactionAttributes"> <props> <prop key="testFlightLeg">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop> </props> </property> </bean> <bean id="flightServiceTarget" class="com.ngc.dts.dao.hibernate.FlightLegHDaoTest" init-method="setUp"/> </beans>
When this executes, Spring is opening a Hibernate session, creating a DB connection, running the SQL statement and closing the session for each SQL statement. This one method has 7 SQL statements and Spring is repeating this process 7 times. If I get a SQL exception which causes a rollback, it's only rolling back the one SQL statement instead of all of the insert and update statements generated within the method.Code:package my.package; public class FlightLegHDaoTest implements ApplicationContextAware { private AirportDao aDao; private CarrierDao cDao; private FlightLegDao flDao; private ApplicationContext applicationContext; protected static final DtsLogService logger = MyLogServiceFactory.createLogService(MyTestCase.class); public void setUp() { /* get DAOs from context */ } public void testFlightLeg() { FlightLegImpl fl = new FlightLegImpl(); AirSegment as1 = new AirSegment(); ... //set data to object fl.addAirSegment(as1); AirSegment as2 = new AirSegment(); ... //set data to object fl.addAirSegment(as2); flDao.createFlightLeg(fl); logger.debug("createFlightLeg: " + fl); } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }
The Spring log output for what should be one transaction:
What do I have wrong in my transaction setup that is preventing this from running in a transaction?Code:Opening Hibernate Session Creating new JDBC Connection to [jdbc:mysql:///iris] Hibernate: select carrier0_.AIRCARRIER_ID as AIRCARRIER1_, carrier0_.altName as altName5_, carrier0_.code as code5_, carrier0_.name as name5_ from AirCarrier carrier0_ where carrier0_.code=? Eagerly flushing Hibernate session Closing Hibernate Session Looking up default SQLErrorCodes for DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@29ce8c] Database product name found in cache for DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@29ce8c]: name is 'MySQL' SQL error codes for 'MySQL' found Opening Hibernate Session Creating new JDBC Connection to [jdbc:mysql:///iris] Hibernate: select airport0_.AIRPORT_ID as AIRPORT1_, airport0_.city as city3_, airport0_.country as country3_, airport0_.iata as iata3_, airport0_.icao as icao3_, airport0_.name as name3_, airport0_.state as state3_ from Airport airport0_ where airport0_.iata=? Eagerly flushing Hibernate session Closing Hibernate Session Opening Hibernate Session Creating new JDBC Connection to [jdbc:mysql:///iris] Hibernate: select airport0_.AIRPORT_ID as AIRPORT1_, airport0_.city as city3_, airport0_.country as country3_, airport0_.iata as iata3_, airport0_.icao as icao3_, airport0_.name as name3_, airport0_.state as state3_ from Airport airport0_ where airport0_.iata=? Eagerly flushing Hibernate session Closing Hibernate Session Looking up default SQLErrorCodes for DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@29ce8c] Database product name found in cache for DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@29ce8c]: name is 'MySQL' SQL error codes for 'MySQL' found Opening Hibernate Session Creating new JDBC Connection to [jdbc:mysql:///iris] Hibernate: insert into FlightLeg (index_col) values (?) Hibernate: insert into AirSegment (arrivalTime, carrier_fk, departureTime, destination_airport_fk, equipmentCode, flightNumber, origin_airport_fk, index_col) values (?, ?, ?, ?, ?, ?, ?, ?) Eagerly flushing Hibernate session Hibernate: update AirSegment set flightLeg_fk=?, index_col=? where AIRSEGMENT_ID=? Closing Hibernate Session Opening Hibernate Session Creating new JDBC Connection to [jdbc:mysql:///iris] Hibernate: insert into FlightLeg (index_col) values (?) Hibernate: insert into AirSegment (arrivalTime, carrier_fk, departureTime, destination_airport_fk, equipmentCode, flightNumber, origin_airport_fk, index_col) values (?, ?, ?, ?, ?, ?, ?, ?) Eagerly flushing Hibernate session Closing Hibernate Session


Reply With Quote