当前位置: 动力学知识库 > 问答 > 编程问答 >

mysql - How to lock access to a database while using it in java by Hibernate?

问题描述:

I'm using JPA/HIBERNATE to manage my mysql database (read & write), the same database is used by other applications (read only).

What I want to do is : While I'm editing my db in java (hibernate) I want all other applications can read the old values, and when my program finishes, I want to switch to the newer values modified by the program.

How can I do this ?

Here is the code

public static Integer (Equipment equipment)

{

Session session = factory.openSession();

Transaction tx = null;

Integer equipementID = null;

try{

tx = session.beginTransaction();

equipementID = (Integer) session.save(equipment);

tx.commit();

}catch (ConstraintViolationException e) {

if (tx!=null) tx.rollback();

}finally {

}

return equipementID;

}

public static void parseFile (String filePath1) throws IOException

{

Scanner s1 = new Scanner(new File(filePath1));

s1.useDelimiter("\\s*fish\\s*");

while (s1.hasNextLine())

{

Scanner s2 = new Scanner (s1.nextLine());

s2.useDelimiter(";");

while (s2.hasNext()) {

String hostName = s2.next();

int asNum=s2.nextInt();

String type=s2.next();

String addrIp=s2.next();

String vendor=s2.next();

String model=s2.next();

String version=s2.next();

String function=s2.next();

String usage=s2.next();

if (asNum==3215)

addEquipmentToDatabase(new Equipment(hostName,0,addrIp,"ftipnn",asNum,model,type,vendor,function,usage,version,1,0));

else

updateEquipment(equipment,new Equipment(hostName,0,addrIp,"domain",asNum,model,type,vendor,function,usage,version,1,0));

}

}

s2.close();

}

s1.close();

}

public static void updateEquipment (Equipment actualEquipment,Equipment newEquipment)

{

Transaction tx = session.beginTransaction();

Query query = session.createSQLQuery("UPDATE equipment SET hostName = '"+newEquipment.getHostName()+"',"

+"parity= '"+newEquipment.getParity()+"',"

+"addrIP= '"+newEquipment.getAddrIP()+"',"

+"domainExtension= '"+newEquipment.getDomainExtension()+"',"

+"asNum= '"+newEquipment.getAsNum()+"',"

+"model= '"+newEquipment.getModel()+"',"

+"type= '"+newEquipment.getType()+"',"

+"vendor ='"+newEquipment.getVendor()+"',"

+"function= '"+newEquipment.getFunction()+"',"

+"utilization= '"+newEquipment.getUtilization()+"',"

+"version= '"+newEquipment.getVersion()+"',"

+"idSite= '"+newEquipment.getIdSite()+"',"

+"idProfile= '"+newEquipment.getIdProfile()+"' "+

"WHERE hostName LIKE '"+actualEquipment.getHostName()+"'") ;

query.executeUpdate();

tx.commit();

}

网友答案:

What you describe involves setting the appropriate ISOLATION level.

There are four different types of isolation levels:

  • Read uncommitted
  • Read committed
  • Repeatable Read
  • Serializable

Most databases typically perform read committed by default, which means that changes by another transaction will not be visible to any other query until that transaction has committed its work however reads are permitted to read the existing (no changed data).

The downside to read committed is that if enough rows are being altered by one transaction, those locks can be escalated to a higher level (page or table) and cause performance bottleneck issues. This is often why it's recommended that during bulk operations that you execute small commit points along the way to keep the table from being locked for other transactions.

MySQL is a tad different as it's default is repeatable read.

The difference betweeen repeatable reads versus read committed is that MySQL will hold onto a read lock until the end of the transaction where-as other databases will release a read lock immediately on a resource. In other words, your read operations by other applications could potentially impact your write ability in your write application if the read applications perform long running transactions or vice versa.

分享给朋友:
您可能感兴趣的文章:
随机阅读: