Announcement Announcement Module
Collapse
No announcement yet.
TransactionSystemException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • TransactionSystemException

    Hello,
    when i'm running my Junit test or main class, not able to save the data in database getting the following error:
    Code:
     testMethod = testSave@MotJunitTest, testException = org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction, mergedContextConfiguration = [MergedContextConfiguration@c2b07 testClass = MotJunitTest, locations = '{classpath:/com/af/mot/jUnit/web-application-context.xml}',
    My Service:

    Code:
    @Service
    public class MyService {
    	
    	private Logger logger = LoggerFactory.getLogger(getClass());
    
    	@Autowired
    	private CompanyDao companyDao;
    
    	/**
    	 * SaveOrUpdate Company
    	 * @param Company
    	 * @return Company
    	 */
    	@Transactional
    	public Company saveOrUpdateCompany(Company comp) throws Exception {
    		Company company = null;
    		try{
    			company =  companyDao.saveOrUpdateCompany(comp);
    		}catch(Exception e){
    			ExceptionLogUtils.logMotsBusinesLayerException(e, logger);
    		}
    		return company;
    	}
    }
    MyDao

    Code:
    @Repository
    public class MyDao extends BaseDao {
    	private Logger logger = LoggerFactory.getLogger(getClass());
    	
    	
    	/**
    	 * saveOrUpdate Company
    	 * @param Company
    	 * @return Company
    	 */
    	
    	public Company saveOrUpdateCompany(Company comp) throws MOTDataException {
    		getEntityManager().persist(comp);
    		return comp;
    
    	}
    }
    My BaseDao

    Code:
    @Repository
    public abstract class BaseDao {
    
    	@Autowired
    	public QueriesUtil queries;
    	
    	@PersistenceContext
    	private EntityManager entityManager;
    
    	/**
    	 * @return the entityManager
    	 */
    	public EntityManager getEntityManager() {
    		return entityManager;
    	}
    
    	/**
    	 * @param entityManager
    	 * the entityManager to set
    	 */
    	public void setEntityManager(EntityManager entityManager) {
    		this.entityManager = entityManager;
    	}
    }
    My Junit

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"web-application-context.xml"})
    
    public class MotJunitTest {
    
    	private Logger logger = LoggerFactory.getLogger(getClass());
    	
    	@Autowired
    	private CompanyService companyService;
    	
    	@Test
    	public void testSave() throws Exception{
    		Company comp = new Company();
    		comp.setCompanyUniqueName("xxx");
    		comp.setAddress("xxx");
    		comp.setContactPersonName("xxxx");
    		comp.setContactPersonEmailAddress("[email protected]");
    		comp.setName("dfdsfdsf");
    		comp.setCreatedBy("dfdsf");
    		comp.setStatus("X");
    		comp.setCreatedOn(sfdsfdf);
    		Company company = companyService.saveOrUpdateCompany(comp);
    		assertEquals(company , comp);
    	}
    	
    }
    application-context.xml

    HTML 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"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:cache="http://www.springframework.org/schema/cache"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="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.xsd
                               http://www.springframework.org/schema/util 
                               http://www.springframework.org/schema/util/spring-util-3.0.xsd
                               http://www.springframework.org/schema/cache 
                               http://www.springframework.org/schema/cache/spring-cache.xsd
                               http://www.springframework.org/schema/tx 
                               http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                               http://www.springframework.org/schema/aop 
                               http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
        
        <context:component-scan base-package="com.af.mot">
        	<context:exclude-filter type="regex" expression="com\.af\.mot\.api.*"/>
        	<context:exclude-filter type="regex" expression="com\.af\.mot\.security\.util.*"/>
        </context:component-scan>
    
    	<!-- External Imports -->
        <tx:annotation-driven transaction-manager="transactionManager" />
        
    	<!-- Load MOT Properties -->
    	<bean id="motProps" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="location" value="classpath:mot.properties"/>		
    	</bean>
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="MOTapplication"></property>
    	</bean>
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
    	
    	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    		 
    </beans>
    Persistence.xml

    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
                 version="1.0">
    	<persistence-unit name="MOTapplication" transaction-type="RESOURCE_LOCAL">
    		<properties>
    			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
    	        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/xxxx" />
    	        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
    	        <property name="javax.persistence.jdbc.user" value="root" />
    	        <property name="javax.persistence.jdbc.password" value="admin" />	        
    	        <property name="hibernate.archive.autodetection" value="class"/>
    	         <property name="hibernate.hbm2ddl.auto" value="update"/>
    		</properties>
    	</persistence-unit>
    </persistence>
    Please help me out from this exception

    Thanks
    Last edited by Sudha Deepthi; May 23rd, 2013, 07:09 AM.

  • #2
    Hi,

    Can you try to add ?

    @Transactional on class level on your test

    You could perhaps put log level of Spring a little bit higher to see what could be the problem ? :-)

    Grtz,

    Sikke
    Last edited by Kristof303; Jun 13th, 2013, 02:20 PM.

    Comment

    Working...
    X