package com.cdai.orm.hibernate.annotation;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "tb_account")
public class Account implements Serializable {
private static final long serialVersionUID = 5018821760412231859L;
@Id
@Column(name = "col_id")
private long id;
@Column(name = "col_balance")
private long balance;
public Account() {
}
public Account(long id, long balance) {
this.id = id;
this.balance = balance;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getBalance() {
return balance;
}
public void setBalance(long balance) {
this.balance = balance;
}
@Override
public String toString() {
return "Account [id=" + id + ", balance=" + balance + "]";
}
}
package com.cdai.orm.hibernate.transaction;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import com.cdai.orm.hibernate.annotation.Account;
public class DirtyRead {
public static void main(String[] args) {
final SessionFactory sessionFactory = new AnnotationConfiguration().
addFile("hibernate/hibernate.cfg.xml").
configure().
addPackage("com.cdai.orm.hibernate.annotation").
addAnnotatedClass(Account.class).
buildSessionFactory();
Thread t1 = new Thread() {
@Override
public void run() {
Session session1 = sessionFactory.openSession();
Transaction tx1 = null;
try {
tx1 = session1.beginTransaction();
System.out.println("T1 - Begin trasaction");
Thread.sleep(500);
Account account = (Account)
session1.get(Account.class, new Long(1));
System.out.println("T1 - balance=" + account.getBalance());
Thread.sleep(500);
account.setBalance(account.getBalance() + 100);
System.out.println("T1 - Change balance:" + account.getBalance());
tx1.commit();
System.out.println("T1 - Commit transaction");
Thread.sleep(500);
}
catch (Exception e) {
e.printStackTrace();
if (tx1 != null)
tx1.rollback();
}
finally {
session1.close();
}
}
};
// 3.Run transaction 2
Thread t2 = new Thread() {
@Override
public void run() {
Session session2 = sessionFactory.openSession();
Transaction tx2 = null;
try {
tx2 = session2.beginTransaction();
System.out.println("T2 - Begin trasaction");
Thread.sleep(500);
Account account = (Account)
session2.get(Account.class, new Long(1));
System.out.println("T2 - balance=" + account.getBalance());
Thread.sleep(500);
account.setBalance(account.getBalance() - 100);
System.out.println("T2 - Change balance:" + account.getBalance());
tx2.commit();
System.out.println("T2 - Commit transaction");
Thread.sleep(500);
}
catch (Exception e) {
e.printStackTrace();
if (tx2 != null)
tx2.rollback();
}
finally {
session2.close();
}
}
};
t1.start();
t2.start();
while (t1.isAlive() || t2.isAlive()) {
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
}
}
System.out.println("Both T1 and T2 are dead.");
sessionFactory.close();
}
}
T1 - Begin trasaction T2 - Begin trasaction Hibernate: select account0_.col_id as col1_0_0_, account0_.col_balance as col2_0_0_ from tb_account account0_ where account0_.col_id=? Hibernate: select account0_.col_id as col1_0_0_, account0_.col_balance as col2_0_0_ from tb_account account0_ where account0_.col_id=? T1 - balance=100 T2 - balance=100 T2 - Change balance:0 T1 - Change balance:200 Hibernate: update tb_account set col_balance=? where col_id=? Hibernate: update tb_account set col_balance=? where col_id=? T1 - Commit transaction T2 - Commit transaction Both T1 and T2 are dead.
select ... for update
package com.cdai.orm.hibernate.transaction;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.cdai.orm.hibernate.annotation.Account;
import com.cdai.orm.hibernate.annotation.AnnotationHibernate;
public class UpgradeLock {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
final SessionFactory sessionFactory = AnnotationHibernate.createSessionFactory();
// Run transaction 1
Thread t1 = new Thread() {
@Override
public void run() {
Session session1 = sessionFactory.openSession();
Transaction tx1 = null;
try {
tx1 = session1.beginTransaction();
System.out.println("T1 - Begin trasaction");
Thread.sleep(500);
Account account = (Account)
session1.get(Account.class, new Long(1), LockMode.UPGRADE);
System.out.println("T1 - balance=" + account.getBalance());
Thread.sleep(500);
account.setBalance(account.getBalance() + 100);
System.out.println("T1 - Change balance:" + account.getBalance());
tx1.commit();
System.out.println("T1 - Commit transaction");
Thread.sleep(500);
}
catch (Exception e) {
e.printStackTrace();
if (tx1 != null)
tx1.rollback();
}
finally {
session1.close();
}
}
};
// Run transaction 2
Thread t2 = new Thread() {
@Override
public void run() {
Session session2 = sessionFactory.openSession();
Transaction tx2 = null;
try {
tx2 = session2.beginTransaction();
System.out.println("T2 - Begin trasaction");
Thread.sleep(500);
Account account = (Account)
session2.get(Account.class, new Long(1), LockMode.UPGRADE);
System.out.println("T2 - balance=" + account.getBalance());
Thread.sleep(500);
account.setBalance(account.getBalance() - 100);
System.out.println("T2 - Change balance:" + account.getBalance());
tx2.commit();
System.out.println("T2 - Commit transaction");
Thread.sleep(500);
}
catch (Exception e) {
e.printStackTrace();
if (tx2 != null)
tx2.rollback();
}
finally {
session2.close();
}
}
};
t1.start();
t2.start();
while (t1.isAlive() || t2.isAlive()) {
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
}
}
System.out.println("Both T1 and T2 are dead.");
sessionFactory.close();
}
}
T1 - Begin trasaction T2 - Begin trasaction Hibernate: select account0_.col_id as col1_0_0_, account0_.col_balance as col2_0_0_ from tb_account account0_ with (updlock, rowlock) where account0_.col_id=? Hibernate: select account0_.col_id as col1_0_0_, account0_.col_balance as col2_0_0_ from tb_account account0_ with (updlock, rowlock) where account0_.col_id=? T2 - balance=100 T2 - Change balance:0 Hibernate: update tb_account set col_balance=? where col_id=? T2 - Commit transaction T1 - balance=0 T1 - Change balance:100 Hibernate: update tb_account set col_balance=? where col_id=? T1 - Commit transaction Both T1 and T2 are dead.
package com.cdai.orm.hibernate.transaction;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;
@Entity
@Table(name = "tb_account_version")
public class AccountVersion {
@Id
@Column(name = "col_id")
private long id;
@Column(name = "col_balance")
private long balance;
@Version
@Column(name = "col_version")
private int version;
public AccountVersion() {
}
public AccountVersion(long id, long balance) {
this.id = id;
this.balance = balance;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getBalance() {
return balance;
}
public void setBalance(long balance) {
this.balance = balance;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
}
T1 - Begin trasaction T2 - Begin trasaction Hibernate: select accountver0_.col_id as col1_0_0_, accountver0_.col_balance as col2_0_0_, accountver0_.col_version as col3_0_0_ from tb_account_version accountver0_ where accountver0_.col_id=? Hibernate: select accountver0_.col_id as col1_0_0_, accountver0_.col_balance as col2_0_0_, accountver0_.col_version as col3_0_0_ from tb_account_version accountver0_ where accountver0_.col_id=? T1 - balance=1000 T2 - balance=1000 T1 - Change balance:900 T2 - Change balance:1100 Hibernate: update tb_account_version set col_balance=?, col_version=? where col_id=? and col_version=? Hibernate: update tb_account_version set col_balance=?, col_version=? where col_id=? and col_version=? T1 - Commit transaction 2264 [Thread-2] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.cdai.orm.hibernate.transaction.AccountVersion#1] at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1934) at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2578) at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2478) at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2805) at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:114) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:260) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:180) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137) at com.cdai.orm.hibernate.transaction.VersionLock$2.run(VersionLock.java:93) Both T1 and T2 are dead.
package com.cdai.orm.hibernate.query;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;
import com.cdai.orm.hibernate.annotation.Account;
public class BasicQuery {
public static void main(String[] args) {
SessionFactory sessionFactory = new AnnotationConfiguration().
addFile("hibernate/hibernate.cfg.xml").
configure().
addPackage("com.cdai.orm.hibernate.annotation").
addAnnotatedClass(Account.class).
buildSessionFactory();
Session session = sessionFactory.openSession();
// 1.HQL
Query query = session.createQuery("from Account as a where a.id=:id");
query.setLong("id", 1);
List result = query.list();
for (Object row : result) {
System.out.println(row);
}
// 2.QBC
Criteria criteria = session.createCriteria(Account.class);
criteria.add(Expression.eq("id", new Long(2)));
result = criteria.list();
for (Object row : result) {
System.out.println(row);
}
// 3.QBE
Account example= new Account();
example.setBalance(100);
result = session.createCriteria(Account.class).
add(Example.create(example)).
list();
for (Object row : result) {
System.out.println(row);
}
// 4.SQL
query = session.createSQLQuery(
" select top 10 * from tb_account order by col_id desc ");
result = query.list();
for (Object row : result) {
System.out.println(Arrays.toString((Object[]) row));
}
session.close();
}
}
Hibernate: select account0_.col_id as col1_0_, account0_.col_balance as col2_0_ from tb_account account0_ where account0_.col_id=? Account [id=1, balance=100] Hibernate: select this_.col_id as col1_0_0_, this_.col_balance as col2_0_0_ from tb_account this_ where this_.col_id=? Account [id=2, balance=100] Hibernate: select this_.col_id as col1_0_0_, this_.col_balance as col2_0_0_ from tb_account this_ where (this_.col_balance=?) Account [id=1, balance=100] Account [id=2, balance=100] Hibernate: select top 10 * from tb_account order by col_id desc [2, 100] [1, 100]
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有