📄 businessservice.java
字号:
package com.ghy.test9;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*;
public class BusinessService extends Thread{
public static SessionFactory sessionFactory;
static{
try{
// Create a configuration based on the properties file we've put
Configuration config = new Configuration();
config.addClass(User.class);
// Get the session factory we can use for persistence
sessionFactory = config.buildSessionFactory();
}catch(Exception e){e.printStackTrace();}
}
private String transactionType;
private Log log;
public BusinessService(String transactionType,Log log){
this.transactionType=transactionType;
this.log=log;
}
public void run(){
try{
if(transactionType.equals("cut"))
cut();
else
add();
}catch(Exception e){
e.printStackTrace();
}
}
public void cut() throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
log.write("cut():开始事务");
Thread.sleep(500);
Query query = session.createQuery("from User as c where c.name=:name");
query.setString("name", "Tom");
query.setLockMode("c", LockMode.UPGRADE);
List list =query.list();
User user=(User)list.get(0);
log.write("cut():查询到:number="+user.getNumber());
Thread.sleep(500);
user.setNumber(user.getNumber()-100);
log.write("ctu():number-100元=:"+user.getNumber());
log.write("cut():提交事务");
tx.commit();
Thread.sleep(500);
}catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
} finally {
// No matter what, close the session
session.close();
}
}
public void add() throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
log.write("add():开始事务");
Thread.sleep(500);
Query query = session.createQuery("from User as c where c.name=:name");
query.setString("name", "Tom");
query.setLockMode("c", LockMode.UPGRADE);
List list =query.list();
User user=(User)list.get(0);
log.write("add():查询number="+user.getNumber());
Thread.sleep(500);
user.setNumber(user.getNumber()+100);
log.write("add(): number+100 ===:"+user.getNumber());
log.write("add():提交事务");
tx.commit();
Thread.sleep(500);
}catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
} finally {
// No matter what, close the session
session.close();
}
}
public void createUser() throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
User user=new User();
user.setName("Tom");
user.setNumber(1000);
session.save(user);
tx.commit();
}catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
} finally {
// No matter what, close the session
session.close();
}
}
public static void main(String args[]) throws Exception {
Log log=new Log();
Thread addThread=new BusinessService("add",log);
Thread cutCheckThread=new BusinessService("cut",log);
((BusinessService)addThread).createUser();
addThread.start();
cutCheckThread.start();
while(addThread.isAlive() ||cutCheckThread.isAlive()){
Thread.sleep(100);
}
log.print();
sessionFactory.close();
}
}
class Log{
private ArrayList logs=new ArrayList();
synchronized void write(String text){
logs.add(text);
}
public void print(){
for (Iterator it = logs.iterator(); it.hasNext();) {
System.out.println(it.next());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -