I am facing a problem that when I use hibernate+spring+mysql to commiting my data from pages to database,the transaction didn't commit. Or perhaps my configuration is somewhere wrong.
The following is my configuration and code:
1.spring-config.xml(The configuration of spring)
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/webtest?charset=UTF-8" />
<property name="user" value="root" />
<property name="password" value="kevin" />
<property name="initialPoolSize" value="5"/>
<property name="minPoolSize" value="5"/>
<property name="maxPoolSize" value="15" />
<property name="checkoutTimeout" value="1000" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.kevin" />
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">select</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<!-- <prop key="connection.autocommit">true</prop> -->
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>
<tx:annotation-driven />
</bean>
2.User.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
*@author kevin
*@date 2015年5月29日 上午11:24:17
*
**/
@Entity
@Table(name="sys_user")
public class User implements Serializable{
private static final long serialVersionUID = -8693332653054586507L;
@Id
@Column(name="user_id")
private String id;
@Column(name="user_real_name")
private String name;
@Column(name="user_education")
private String edu;
getter()setter()...
}
3.UserService.java
@Service
public class UserService {
@Resource
private UserDao userDao;
@org.springframework.transaction.annotation.Transactional
public boolean createUser(User user) {
try {
user.setId(String.valueOf(new Date().getTime()));
userDao.add(user);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
4.UserDao.java
@Repository
public class UserDao extends BaseDao<User, String>{
public UserDao() {
super(User.class);
}
}
5.BaseDao.java
public abstract class BaseDao<T, PK extends Serializable> {
private SessionFactory sessionFactory;
private Class<T> cls;
public BaseDao(Class<T> t) {
cls = t;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
//@Transactional
public void add(T t) throws Exception {
sessionFactory.getCurrentSession().save(t);
//sessionFactory.getCurrentSession().flush();
}
}
The code above is just a test code, but it cannot persist to the database. Of course, if I use session.flush()
,it can commit to the database,but I think it is not a good way, because it cannot ensure the consistent of the transaction.
So I am puzzling heavy what wrong with my test and code.
– user3839014 May 29 '15 at 09:49