Results 1 to 5 of 5

Thread: why not in transaction

  1. #1
    Join Date
    Mar 2012
    Posts
    3

    Default why not in transaction

    hi,i'm new in hibernate and spring,this is my config,but its not in a transaction,it seems has no transaction,
    applicationContext.xml file:
    Code:
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
    <property name="configLocation" value="classpath:jbpm.hibernate.cfg.xml" />
    <property name="dataSource" ref="dataSource" /> 
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="*"/>
    </tx:attributes>
    </tx:advice>
    
    <aop:config>
    <aop:pointcut expression="execution(* com.test.TransAction.*(..))" id="txPointcut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
    
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@172.17.1.1:1521:database" /> 
    <property name="username" value="test" />
    <property name="password" value="test" />
    </bean>
    
    <bean id="transAction" class="com.test.TransAction">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    hibernate file:
    Code:
    <hibernate-configuration>
    <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> 
    <property name="hibernate.format_sql">false</property>
    <property name="hibernate.jdbc.batch_size">0</property> 
    
    </session-factory>
    </hibernate-configuration>
    test class:
    Code:
    public class TransAction extends HibernateDaoSupport { 
    private SessionFactory sessionFactory;
    
    
    public void testTrans3() throws RuntimeException{
    Session session=this.getSession();
    
    
    SQLQuery query1=session.createSQLQuery("update deployment set timestamp_='20120219'");
    query1.executeUpdate();
    
    SQLQuery query=session.createSQLQuery("update deployment set timestamp_='20120301089089058795789'");
    query.executeUpdate(); 
    
    }
    }
    when it runs,the first sql is updated,and the second sql throws exception,but first sql cannt rollback,what's wrong with my config,can anyone help me? thanks a lot
    Last edited by wjz290; Mar 5th, 2012 at 05:23 AM. Reason: add code tag

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Please use [ code][/code ] tags when posting code, that way it remains readable. Post the code you use to test your application.

    But on first sight you use a wrong way of obtaining a session. First don't extend HibernateDaoSupport, it should be considered deprecated as the HibernateTemplate. To get a session simply call getCurrentSession on the SessionFactory (which you should inject into your dao).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Mar 2012
    Posts
    3

    Default

    Code:
    public class SpringTest  {
    	public static void main(String args[]){
    		SpringTest test=new SpringTest();
    		test.init();
    	}  
    	BeanFactory factory;
    	public void init(){ 
    	 
    		ClassPathResource resource = new ClassPathResource("applicationContext.xml");
    		factory = new XmlBeanFactory(resource);
    		TransAction  trans=(TransAction)factory.getBean("transAction"); 
    		try {
    			trans.testTrans4();
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	
    	}
    i'm so sorry,here is my test code!
    when i use getCurrenctSession it throws exception
    Code:
    org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    	at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
    	at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:574)
    	at com.test.TransAction.testTrans4(TransAction.java:170)
    	at test.SpringTest.init(SpringTest.java:32)
    	at test.SpringTest.main(SpringTest.java:23)
    Last edited by wjz290; Mar 5th, 2012 at 05:28 AM. Reason: add code tag

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Also read chapter 3 of the reference guide. Use an ApplicationContext instead of a BeanFactory...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Mar 2012
    Posts
    3

    Default

    thanks a lot ,it can works,and i also know the difference between BeanFactory and ApplicationContext ,thanks a lot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •