Hibernate——编程式事务
编程式事务
一、getCurrentSession()与openSession()的区别?
* 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()创建的session则不会
* 采用getCurrentSession()创建的session在commit或rollback时会自动关闭,而采用openSession()创建的session必须手动关闭
二、使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:
* 如果使用的是本地事务(jdbc事务)
<property name="hibernate.current_session_context_class">thread</property>
* 如果使用的是全局事务(jta事务)
<property name="hibernate.current_session_context_class">jta</property>
三、例子:模拟日志管理(添加用户时,写入日志)
因为事务是session.beginTransaction();由Session开启的,所有添加用户和相应的日志,必须在一个事务中,那么就必须使用同一个Seesion,传递太麻烦。采用getCurrentSession()创建的session会绑定到当前线程中变量ThreadLocal中。
User.java
package com.ncepu.usermgr.model; public class User { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
User.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.ncepu.usermgr.model"> <class name="User" table="t_user"> <id name="id"> <generator class="native"/> </id> <property name="name"/> </class> </hibernate-mapping>
Log.java
package com.ncepu.usermgr.model; import java.util.Date; public class Log { private int id; //操作日志、安全日志、事件日志 private String type; private String detail; private Date time; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } public Date getTime() { return time; } public void setTime(Date time) { this.time = time; } }
Log.hbm.xml
<hibernate-mapping package="com.ncepu.usermgr.model"> <class name="Log" table="t_log"> <id name="id"> <generator class="native"/> </id> <property name="type"/> <property name="detail"/> <property name="time"/> </class> </hibernate-mapping>
UserManager接口
package com.ncepu.usermgr.manager; import com.ncepu.usermgr.model.User; public interface UserManager { public void addUser(User user); }
UserManagerImpl实现类
package com.ncepu.usermgr.manager; import java.util.Date; import org.hibernate.Session; import com.ncepu.usermgr.model.Log; import com.ncepu.usermgr.model.User; import com.ncepu.usermgr.util.HibernateUtils; public class UserManagerImpl implements UserManager { public void addUser(User user) { Session session = null; try { //session = HibernateUtils.getSession(); session = HibernateUtils.getSessionFactory().getCurrentSession(); session.beginTransaction(); session.save(user); Integer.parseInt("asdfsdfsfsd"); Log log = new Log(); log.setType("安全日志"); log.setDetail("xxx进入系统"); log.setTime(new Date()); LogManager logManager = new LogManagerImpl(); logManager.addLog(log); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); // }finally { // HibernateUtils.closeSession(session); } } }
LogManager接口
package com.ncepu.usermgr.manager; import com.ncepu.usermgr.model.Log; public interface LogManager { public void addLog(Log log); }
LogManagerImpl实现类
package com.ncepu.usermgr.manager; import com.ncepu.usermgr.model.Log; import com.ncepu.usermgr.util.HibernateUtils; public class LogManagerImpl implements LogManager { public void addLog(Log log) { HibernateUtils.getSessionFactory().getCurrentSession().save(log); } }
Client.java
package com.ncepu.usermgr.client; import com.ncepu.usermgr.manager.UserManager; import com.ncepu.usermgr.manager.UserManagerImpl; import com.ncepu.usermgr.model.User; public class Client { public static void main(String[] args) { User user = new User(); user.setName("张三"); UserManager userManager = new UserManagerImpl(); userManager.addUser(user); } }
工具类SessionFactory
package com.ncepu.usermgr.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtils { private static SessionFactory factory; static { try { Configuration cfg = new Configuration().configure(); factory = cfg.buildSessionFactory(); }catch(Exception e) { e.printStackTrace(); } } public static SessionFactory getSessionFactory() { return factory; } public static Session getSession() { return factory.openSession(); } public static void closeSession(Session session) { if (session != null) { if (session.isOpen()) { session.close(); } } } }
导入数据库工具类
package com.ncepu.usermgr.util; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class ExportDB { public static void main(String[] args) { //读取hibernate.cfg.xml文件 Configuration cfg = new Configuration().configure(); SchemaExport export = new SchemaExport(cfg); export.create(true, true); } }
hibernate.cfg.xml配置文件
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.url">jdbc:mysql://localhost/spring_hibernate_1</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">admin</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.current_session_context_class">thread</property> <!-- <property name="hibernate.current_session_context_class">jta</property> --> <mapping resource="com/ncepu/usermgr/model/User.hbm.xml"/> <mapping resource="com/ncepu/usermgr/model/Log.hbm.xml"/> </session-factory> </hibernate-configuration>