How do synchronized static methods work in Java and can I use it for loading Hibernate entities?(同步静态方法在 Java 中是如何工作的,我可以用它来加载 Hibernate 实体吗?)
问题描述
如果我有一个带有静态方法的 util 类,它将调用 Hibernate 函数来完成基本数据访问.我想知道使方法 synchronized 是否是确保线程安全的正确方法.
If I have a util class with static methods that will call Hibernate functions to accomplish basic data access. I am wondering if making the method synchronized is the right approach to ensure thread-safety.
我希望这可以防止对同一数据库实例的信息访问.但是,我现在确定以下代码是否会阻止在特定类调用 getObjectById 时为所有类调用它.
I want this to prevent access of info to the same DB instance. However, I'm now sure if the following code are preventing getObjectById being called for all Classes when it is called by a particular class.
public class Utils {
public static synchronized Object getObjectById (Class objclass, Long id) {
// call hibernate class
Session session = new Configuration().configure().buildSessionFactory().openSession();
Object obj = session.load(objclass, id);
session.close();
return obj;
}
// other static methods
}
推荐答案
通过在静态方法锁上使用同步,您将 同步类方法和属性(相对于实例方法和属性)
By using synchronized on a static method lock you will synchronize the class methods and attributes ( as opposed to instance methods and attributes )
所以你的假设是正确的.
So your assumption is correct.
我想知道使方法同步是否是确保线程安全的正确方法.
不是真的.您应该让您的 RDBMS 代替它来完成这项工作.他们擅长这种事情.
Not really. You should let your RDBMS do that work instead. They are good at this kind of stuff.
通过同步对数据库的访问,您唯一会得到的就是让您的应用程序变得非常慢.此外,在您发布的代码中,您每次都在构建一个会话工厂,这样,您的应用程序将花费更多时间访问数据库而不是执行实际工作.
The only thing you will get by synchronizing the access to the database is to make your application terribly slow. Further more, in the code you posted you're building a Session Factory each time, that way, your application will spend more time accessing the DB than performing the actual job.
想象以下场景:
客户端 A 和 B 尝试将不同的信息插入到表 T 的记录 X 中.
Client A and B attempt to insert different information into record X of table T.
使用您的方法,您唯一得到的就是确保在数据库中无论如何都会发生这种情况,因为 RDBMS 将阻止他们从 A 插入一半信息,从 B 插入一半信息同一时间.结果将相同,但仅慢 5 倍(或更多).
With your approach the only thing you're getting is to make sure one is called after the other, when this would happen anyway in the DB, because the RDBMS will prevent them from inserting half information from A and half from B at the same time. The result will be the same but only 5 times ( or more ) slower.
也许最好看看 事务和并发" 章节在 Hibernate 文档中.大多数情况下,您试图解决的问题已经得到解决,而且是一种更好的方法.
Probably it could be better to take a look at the "Transactions and Concurrency" chapter in the Hibernate documentation. Most of the times the problems you're trying to solve, have been solved already and a much better way.
这篇关于同步静态方法在 Java 中是如何工作的,我可以用它来加载 Hibernate 实体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:同步静态方法在 Java 中是如何工作的,我可以用它来加载 Hibernate 实体吗?
基础教程推荐
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
