<?xml version="1.0" encoding="UTF-8"?>
<!--
PROPAGATION_REQUIRED——支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
PROPAGATION_SUPPORTS——支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY——支持当前事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW——新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED——以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER——以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED——如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/SSH"></property>
<property name="username" value="root"></property>
<property name="password" value="xyb888890"></property>
<property name="initialSize" value="1"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="2"></property>
<property name="minIdle" value="1"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>com/qdn/SSH/demain/user_hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=create
</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userDaoTarget" class="com.qdn.SSH.dao.impl.UserDao" scope="prototype">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userDao"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" scope="prototype">
<property name="transactionManager" ref="transactionManager"></property>
<property name="target" ref="userDaoTarget" />
<property name="proxyInterfaces" value="com.qdn.SSH.dao.IUser"></property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_NOT_SUPPORTED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
这是Spring的bean配置文件,我想问一下如果不加上述纵横驰红色字体的部份,会不会产生线程安全的问题呢
下面是Dao层的代码:- package com.qdn.SSH.dao.impl;
- import java.util.List;
- import org.hibernate.NonUniqueResultException;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import com.qdn.SSH.dao.IUser;
- import com.qdn.SSH.demain.UserBean;
- public class UserDao implements IUser {
- private SessionFactory sessionFactory;
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
- @Override
- public void addUser(UserBean user) {
- //sessionFactory.getCurrentSession().persist(user);
- System.out.println(sessionFactory);
- }
- @Override
- public void updateUser(UserBean user) {
- sessionFactory.getCurrentSession().merge(user);
- }
- @Override
- public UserBean findUser(int id) {
- return (UserBean) sessionFactory.getCurrentSession().get(
- UserBean.class, id);
- }
- @Override
- public UserBean findUser(String username, String password) {
- return findUser(
- "from UserBean as user where user.username=? and user.password=?",
- username, password);
- }
- @Override
- public UserBean findUser(String hql, Object... args) {
- Query q = sessionFactory.getCurrentSession().createQuery(hql);
- for (int i = 0; i < args.length; i++) {
- q.setParameter(i, args[i]);
- }
- return (UserBean) q.uniqueResult();
- }
- @SuppressWarnings("unchecked")
- @Override
- public List<UserBean> getUsers() {
- return sessionFactory.getCurrentSession().createQuery("from UserBean")
- .list();
- }
- @Override
- public void deleteUser(int id) {
- Session session = sessionFactory.getCurrentSession();
- UserBean user = (UserBean) session.get(UserBean.class, id);
- session.delete(user);
- }
- }
复制代码 |