EnityManagerFactory 单例。请检查一下?
我只是好奇这看起来是否可靠。它没有给出任何错误,但我只是想仔细检查一下,因为我遇到了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码不是“可靠的”:
getInstance()
方法,尽管您需要执行线程安全的初始化。这是因为初始化后,所有需要该实例的线程都必须相互等待(这是一个无用的瓶颈)。仅当您的实例为 null 时,才调用执行初始化的同步(私有)方法;在该方法中,再次检查实例是否为 null。另一种方法是使用一个私有内部类
SingletonHolder
来保存实例,因此您将依赖类加载器来执行线程安全初始化。但是,如果您无法(不想)避免使用单例,那么一个非常好的选择是仅定义一个常量的枚举:INSTANCE;
唯一的缺点是您无法对
INSTANCE
执行延迟初始化,但您现在是线程安全的,并且可以轻松应对序列化或克隆问题。Your code is not "solid":
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;
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.要回答您的问题 - 我认为您应该将构造函数设为私有,以确保其他类无法实例化另一个 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.
代码不是线程安全的。(抱歉,错过了同步
)您不应使用单例。
相反,使用 DI 框架,例如 Spring、guice 或者您的部署环境可能已经提供一。
这将使您的代码更加健壮并且更易于测试。
The code is not thread safe.(Sorry, missed thesynchronized
)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.