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

  • 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, 06:23 AM. Reason: add code tag

  • #2
    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).

    Comment


    • #3
      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, 06:28 AM. Reason: add code tag

      Comment


      • #4
        Also read chapter 3 of the reference guide. Use an ApplicationContext instead of a BeanFactory...

        Comment


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

          Comment

          Working...
          X