Announcement Announcement Module
Collapse
No announcement yet.
Getting Transaction Exception while implementing programatic transaction management. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting Transaction Exception while implementing programatic transaction management.

    I don't know if this is a Spring (2.5) or Hibernate (3.2) issue. I am attempting to implement spring transaction management using Spring's TransactionTemplate class with a dao using hibernates sessionfactory class accessed through the application context. Any insight would be appreciated. I am getting the following exception :

    [exception]
    org.hibernate.TransactionException: Transaction not successfully started
    at org.hibernate.transaction.JDBCTransaction.rollback (JDBCTransaction.java:149)
    at com.tfs.routing.dao.impl.TestTfsDao.testManyToMany Poulation(TestTfsDao.java:75)
    [/exception]
    The invoking class , Service and Dao objects are as follows :
    Code:
    //TestCase
    
    
    public class TestTfsDao extends TestCase {
    
        private static final Logger log = Logger.getLogger(TestTfsDao.class);
    
        private ApplicationContext ctx = null;
        private DialedNumberGroupService dialedNumberGroupService = null;
        private SessionFactory sf = null;
    
        public void setUp() throws Exception {
            super.setUp();
    
            ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
            dialedNumberGroupService = (DialedNumberGroupService) ctx
                    .getBean("DialedNumberGroupService");
            sf = (SessionFactory) ctx.getBean("sessionFactory");
            TransactionHelper.start(sf);
        }
        public void tearDown() throws Exception {
            super.tearDown();
            ctx = null;
            dialedNumberGroupService = null;
            TransactionHelper.stop(sf);
            sf = null;
        }
        public void testManyToManyPoulation() throws Exception {
            RoutingScheme scheme = new RoutingScheme();
            scheme.setCustomers(new HashSet<Customer>(0));
            scheme.setDescription("Description");
            scheme.setPriority(0);
            scheme.setSchemeId("1234");
    
            DialedNumberGroup group = new DialedNumberGroup();
            group.setDescription("Description");
            group.setDialedNumbers(new HashSet<DialedNumber>(0));
            group.setGroupId("1234");
            group.setName("Name");
            group.setRoutingSchemes(new HashSet<RoutingScheme>(0));
            group.getRoutingSchemes().add(scheme);
    
            Session session = sf.getCurrentSession();
            Transaction tx = null;
            try {
                tx = session.beginTransaction();
                dialedNumberGroupService.save(group);
                tx.commit();
                log.info("Transaction Successfully Completed.");
    
            } catch (Exception e) {
                log.info("Transaction Rolled Back.");
                tx.rollback();
                TestCase.fail();
            } finally {
                tx = null;
                session = null;
            }
        }
    }
    //Service Class
    package com.tfs.routing.service.impl;
    
    
    public class DialedNumberGroupServiceImpl implements DialedNumberGroupService {
    
        private static final Logger log = Logger.getLogger(DialedNumberGroupServiceImpl.class);
    
        private TransactionTemplate transactionTemplate;
        private DialedNumberGroupDAO dialedNumberGroupDAO;
    
        public void setTransactionManager(PlatformTransactionManager transactionManager) {
            this.transactionTemplate = new TransactionTemplate(transactionManager);
        }
    
        public DialedNumberGroupDAO getDialedNumberGroupDAO() {
            return dialedNumberGroupDAO;
        }
    
        public void setDialedNumberGroupDAO(DialedNumberGroupDAO dialedNumberGroupDAO) {
            this.dialedNumberGroupDAO = dialedNumberGroupDAO;
        }
    
        @Override
        public void save(final DialedNumberGroup group) {
            log.info("Child collection size = " + group.getRoutingSchemes().size());
            this.transactionTemplate.execute(new TransactionCallbackWithoutResult() {
    
                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    dialedNumberGroupDAO.save(group);
    
                }
            });
    
        }
    }
    //DAO Implementation (Partial)
    
    public class DialedNumberGroupDAOImpl implements DialedNumberGroupDAO {
        private static final Logger log = Logger.getLogger(DialedNumberGroupDAOImpl.class);
        // property constants
        public static final String NAME = "name";
        public static final String DESCRIPTION = "description";
    
        private SessionFactory sessionFactory;
    
        public SessionFactory getSessionFactory() {
            return sessionFactory;
        }
    
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
    
        protected void initDao() {
            // do nothing
        }
    
        public void save(DialedNumberGroup transientInstance) {
            log.debug("saving DialedNumberGroup instance");
            try {
                this.sessionFactory.getCurrentSession().save(transientInstance);
                log.debug("save successful");
            } catch (RuntimeException re) {
                log.error("save failed", re);
                throw re;
            }
        }
    Application Context :

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    	xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    	   <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
    		      <property name="driverClassName"
    			       value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
    		      </property>
    		      <property name="url"
    			       value="jdbc:sqlserver://LORETTA\SQLEXPRESS">
    		      </property>
    		      <property name="username" value="tfs"></property>
    		      <property name="password" value="tfs"></property>
    	   </bean>	   
    	   <bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    		      <property name="dataSource">
    			       <ref bean="datasource" />
    		      </property>
    		      <property name="hibernateProperties">
    			     <props>
    				      <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
    				      <prop key="hibernate.show_sql">true</prop>
    			     </props>
    		      </property>
    		      <property name="mappingResources">
    		      	<list>
    		      		<value>
    		      			com/tfs/routing/model/DialedNumberGroup.hbm.xml
    		      		</value>
    		      		<value>
    		      			com/tfs/routing/model/RoutingScheme.hbm.xml
    		      		</value> 
    		      		</list>
    		      </property>
    	      		      
    		      </bean>
    		      
    	   <bean id="DialedNumberGroupDAO" 	class="com.tfs.routing.dao.impl.DialedNumberGroupDAOImpl">
    	   	<property name="sessionFactory">
    	   		<ref bean="sessionFactory" />
    	   	</property>
    	   </bean>
    	   
    	   <bean id="RoutingSchemeDAO"	class="com.tfs.routing.dao.impl.RoutingSchemeDAOImpl">
    	   	<property name="sessionFactory">
    	   		<ref bean="sessionFactory" />
    	   	</property>
    	   </bean>
    	   
    	   <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
         </bean>
         
         <bean id="DialedNumberGroupService" class="com.tfs.routing.service.impl.DialedNumberGroupServiceImpl">
            <property name="transactionManager" ref="transactionManager"/>
            <property name="dialedNumberGroupDAO" ref="DialedNumberGroupDAO"/>
          </bean>
    	   </beans>
    Mapping Files :

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping>
        <class name="com.tfs.routing.model.DialedNumberGroup" table="dialed_number_group" schema="dbo" catalog="tfs">
            <id name="groupId" type="java.lang.String">
                <column name="GROUP_ID" length="50" />
                <generator class="assigned" />
            </id>
            <property name="name" type="java.lang.String">
                <column name="NAME" length="50" not-null="false" />
            </property>
            <property name="description" type="java.lang.String">
                <column name="DESCRIPTION" length="50" />
            </property>
            <set name="dialedNumbers" inverse="false">
                <key>
                    <column name="GROUP_ID" length="50" not-null="false" />
                </key>
                <one-to-many class="com.tfs.routing.model.DialedNumber" />
            </set>
            <set name="routingSchemes" table="scheme_entry" inverse="true">
              <key column="Group_ID"/>
              <many-to-many column="SCHEME_ID" class="com.tfs.routing.model.RoutingScheme"/>
            </set>    
        </class>
    
        <class name="com.tfs.routing.model.RoutingScheme" table="routing_scheme" schema="dbo" catalog="tfs">
            <id name="schemeId" type="java.lang.String">
                <column name="SCHEME_ID" length="50" />
                <generator class="assigned" />
            </id>
            <property name="priority" type="java.lang.Integer">
                <column name="PRIORITY" not-null="false" />
            </property>
            <property name="description" type="java.lang.String">
                <column name="DESCRIPTION" length="200" />
            </property>
            <set name="customers" inverse="false">
                <key>
                    <column name="OVERRIDE_SCHEME" length="50" not-null="false" />
                </key>
                <one-to-many class="com.tfs.routing.model.Customer" />
            </set>      
            <set name="dialedNumberGroups" table="scheme_entry"  cascade="all">
              <key column="Group_ID"/>
              <many-to-many column="GROUP_ID" class="com.tfs.routing.model.DialedNumberGroup"/>
            </set>      
        </class>
    </hibernate-mapping>
Working...
X