Spring 4 hibernate 4 @Transactional(只读=“true”)不起作用

时间:2014-11-28 13:49:41

标签: java spring

我使用hibernate 4,spring 4,我想使用@Transaction注释,但它不起作用。

用户对象仍保存在sqlServer上。

知道我做错了吗?


[applicationContext.xml]

<tx:annotation-driven transaction-manager="transactionManager" />

<context:component-scan base-package="SpringDAO"/>
<context:component-scan base-package="SpringTest"/>
<context:component-scan base-package="service"/>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"
    p:sessionFactory-ref="sessionFactory" />

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation">
        <value>hibernate.cfg.xml</value>
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">false</prop>
            <prop key="hibernate.generate_statistics">true</prop>
        </props>
    </property>
</bean>

<bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
    <property name="driverClassName"> 
       <value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value> 
    </property> 
    <property name="url"> 
        <value>jdbc:sqlserver://127.0.0.1:1433;databaseName=test</value> 
    </property>
    <property name="username"> 
        <value>XXX</value> 
    </property> 
    <property name="password"> 
        <value>XXX</value> 
    </property> 
 </bean>

[hibernate.cfg.xml]

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration> 
    <session-factory> 

        <mapping class="SpringDAO.User" />

    </session-factory> 
</hibernate-configuration>

package SpringDAO;

@Repository("UserDAO3")
public class UserDAO3 {

SessionFactory sessionFactory;

@Autowired
public void setSessionFactory(SessionFactory value){
    this.sessionFactory = value;
}

public SessionFactory getSessionFactory(){
    return this.sessionFactory;
}

@Transactional(readOnly = true)
public boolean insert(Object user){
    Session sess = this.sessionFactory.getCurrentSession();
    sess.save(user);
    return true;
}
}

package service;

@Service("UserService")
public class UserService {

    public boolean addAction(User user){
        boolean result = true;
        UserDAO3 dao = (UserDAO3)SpringUtil.getBean("UserDAO3");
        List<User> users = dao.searchAllUser();

        for(User selectedUser : users){
            if(selectedUser.getName().equals(user.getName())){
                result = false;
                break;
            }
        }
        result = dao.insert(user);
        return result;
    }
}

3 个答案:

答案 0 :(得分:0)

来自docs

  

这只是   作为实际交易子系统的提示;它不会   必然导致写访问尝试失败。交易   无法解释只读提示的管理器不会抛出   当被要求进行只读交易时会出现异常。

事务的隔离级别是否可以更改取决于实现。根据驱动程序可能会发生不同的事情,无法保证,它不会强制执行失败。

答案 1 :(得分:0)

POJO:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;

修饰:

@Id
@Column(name = "id")
private Integer id;

消除@GeneratedValue(strategy = GenerationType.AUTO)

后,它可以正常工作

答案 2 :(得分:0)

在ibm.com上查看@Transactional read-only flag pitfalls,解释真的很棒。你的问题在那里得到解答。

相关问题