Announcement Announcement Module
Collapse
No announcement yet.
Hibernate: data is not commited Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate: data is not commited

    Hello dear community.

    I am trying to use declarative transaction management with Spring and Hibernate. Here is my configuration:

    Code:
      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url" value="${database.url}" />
        <property name="driverClassName" value="${database.driver}" />
        <property name="username" value="${database.user}" />
        <property name="password" value="${database.password}" />
        <property name="initialSize" value="${database.pool.initialSize}" />
        <property name="maxActive" value="${database.pool.maxActive}" />
      </bean>
    
      <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="hibernateSessionFactory" />
      </bean>
    
      <bean id="hibernateSessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
        <property name="dataSource" ref="dataSource" />
      </bean>
    
      <aop:config>
        <aop:pointcut id="txServices" expression="@within(org.springframework.stereotype.Service)" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txServices" />
      </aop:config>
    
      <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
          <tx:method name="get*" read-only="true" />
          <tx:method name="*" />
        </tx:attributes>
      </tx:advice>
    You can see that all beans annotated with @Service must have transaction aspect (and they have as JDK-proxies are created at runtime).

    In my @Service annotated class i have the following method:

    Code:
        @Autowired
        private SessionFactory sessionFactory;
    
        public void helloHibernateWorld() {
            Message message = new Message("Hello World!");
            Long id = (Long) sessionFactory.getCurrentSession().save(message);
            logger.info("MESSAGE_ID: " + id);
        }
    Here is my hibernate.cfg.xml:

    Code:
    <hibernate-configuration>
      <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.transaction.auto_close_session">true</property>
        <property name="hibernate.transaction.flush_before_completion">true</property>
    
        <mapping resource="Message.hbm.xml" />
    
      </session-factory>
    </hibernate-configuration>
    My problem is that there is no changes in the database after invoking this method, all i am seeing is

    Code:
    Hibernate: select max(MESSAGE_ID) from MESSAGES
    // but no insert here
    It looks like transaction is not commited or perhaps does not exist but I cannot find troubles with my configuration. Please, help me.

  • #2
    Hm... It now works with my org.apache.commons.dbcp.BasicDataSource, but not with datasource that I defined in tomcat:

    Code:
    <jee:jndi-lookup id="dataSource" jndi-name="${datasource.jndi-name}" resource-ref="true" />
    Code:
    <Resource name="jdbc/Mysql" auth="Container" type="javax.sql.DataSource"
                  username="dbuser"
                  password="dbpassword"
                  driverClassName="com.mysql.jdbc.Driver"
                  url="jdbc:mysql://localhost:3306/db?autoReconnect=true" />

    Comment


    • #3
      This is my mistake, i tryed to save data from the method with read-only semantics.

      Comment

      Working...
      X