EnityManagerFactory 单例。请检查一下?

发布于 2024-11-07 17:43:09 字数 768 浏览 0 评论 0原文

我只是好奇这看起来是否可靠。它没有给出任何错误,但我只是想仔细检查一下,因为我遇到了 c3p0 的池问题。只是检查一下是否有任何原因。先感谢您!

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;


public class EntityManagerFactorySingleton {
    private static EntityManagerFactorySingleton singleton;
    private EntityManagerFactory emf;

public EntityManagerFactorySingleton(){
    emf = Persistence.createEntityManagerFactory(ConfigList.getProperty(Config.PERSISTENCE_UNIT), System.getProperties());
}

public synchronized static EntityManagerFactorySingleton getInstance() {
   if(singleton == null) {
    singleton = new EntityManagerFactorySingleton();
   }
   return singleton;
}

public EntityManagerFactory getEntityManagerFactory(){
    return emf;
}

}

I am just cruious if this looks solid. It gives no errors but I just want to double check as I am having a pooling issue with c3p0. Just checking to see if anything here is the cause. Thank you in advance!

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;


public class EntityManagerFactorySingleton {
    private static EntityManagerFactorySingleton singleton;
    private EntityManagerFactory emf;

public EntityManagerFactorySingleton(){
    emf = Persistence.createEntityManagerFactory(ConfigList.getProperty(Config.PERSISTENCE_UNIT), System.getProperties());
}

public synchronized static EntityManagerFactorySingleton getInstance() {
   if(singleton == null) {
    singleton = new EntityManagerFactorySingleton();
   }
   return singleton;
}

public EntityManagerFactory getEntityManagerFactory(){
    return emf;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江湖彼岸 2024-11-14 17:43:09

您的代码不是“可靠的”:

  • 构造函数对于单例来说必须是私有的,
  • 您不应该同步 getInstance() 方法,尽管您需要执行线程安全的初始化。这是因为初始化后,所有需要该实例的线程都必须相互等待(这是一个无用的瓶颈)。
    仅当您的实例为 null 时,才调用执行初始化的同步(私有)方法;在该方法中,再次检查实例是否为 null。另一种方法是使用一个私有内部类 SingletonHolder 来保存实例,因此您将依赖类加载器来执行线程安全初始化。

但是,如果您无法(不想)避免使用单例,那么一个非常好的选择是仅定义一个常量的枚举:INSTANCE;

public enum EntityManagerFactorySingleton {
    INSTANCE;

    // all your code -- fields, constructor, instance / static methods get in here
    // you can still have the `getInstance()` static method returning INSTANCE, if you want.
}

唯一的缺点是您无法对 INSTANCE 执行延迟初始化,但您现在是线程安全的,并且可以轻松应对序列化或克隆问题。

Your code is not "solid":

  • constructor must be private for a singleton
  • you shouldn't have the getInstance() method synchronised, although you need to perform the initialisation thread-safe. That's because after initialization, all the threads that need the instance will have to wait for each other (and that's a useless bottleneck).
    Only if your instance is null, call a synchronized (private) method that performs the initialisation; inside that method, check again if the instance is null. Another approach is to have a private inner class SingletonHolder that holds the instance, so you'll rely on the class-loader for performing the thread-safe initialisation.

However, if you can't (don't want to) avoid using a singleton, a very good choice would be an enum with only one constant defined: INSTANCE;

public enum EntityManagerFactorySingleton {
    INSTANCE;

    // all your code -- fields, constructor, instance / static methods get in here
    // you can still have the `getInstance()` static method returning INSTANCE, if you want.
}

The only drawback is that you cannot perform lazy initialisation for the INSTANCE, but you're now thread-safe and ready for serialization or cloning issues without any effort.

一指流沙 2024-11-14 17:43:09

回答您的问题 - 我认为您应该将构造函数设为私有,以确保其他类无法实例化另一个 EntityManagerFactorySingleton。但是,如果这种情况没有发生,那么我看不出这个类会导致池化问题的原因。

To answer your question - I think you should make the constructor private, to ensure other Classes cannot instantiate another EntityManagerFactorySingleton. However, if that is not happening, then I can see no reason why this class would be causing a pooling issue.

飞烟轻若梦 2024-11-14 17:43:09
  1. 代码不是线程安全的。(抱歉,错过了同步

  2. 您不应使用单例

相反,使用 DI 框架,例如 Springguice 或者您的部署环境可能已经提供一。

这将使您的代码更加健壮并且更易于测试。

  1. The code is not thread safe. (Sorry, missed the synchronized)

  2. You should not use singletons.

Instead, use a DI framework like Spring, guice or maybe your deployment envionment already offers one.

This will make your code much more robust and much more simple to test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文