我是Hibernate的新手。我创建了SessionFactory并获得了如下的Session。我正在使用c3p0连接提供程序
public static SessionFactory getSessionFactory() {
try {
if (sessionFactory == null) {
Configuration configuration = loadDBConfiguration();
if (configuration != null) {
sessionFactory = configuration.buildSessionFactory();
} else {
log.info("---- Configuration failed ----");
}
}
} catch (Exception ex) {
log.info("---- Initial SessionFactory creation failed ----");
}
return sessionFactory;
}
public static EntityManagerFactory getEntityManagerFactory() {
try {
Session session = getSessionFactory().openSession();
entityManagerFactory = session.getEntityManagerFactory();
} catch (Exception e) {
log.error(e);
}
return entityManagerFactory;
}
public static EntityManager getEntityManager() {
try {
EntityManagerFactory entityManagerFactory = getEntityManagerFactory();
return entityManagerFactory.createEntityManager();
} catch (Exception e) {
log.error(e);
}
return null;
}
我对以下事情感到困惑。
目前,我的所有EntityManager类(如UserManager,AccountsManager)都会调用getEntityManager()。在RDS中经过一段时间后,它显示为20个连接,甚至我的应用程序也没有处理任何用户请求。
答案 0 :(得分:0)
你应该关闭你的连接。由于您的连接由数据库连接池包装,因此它们不会被物理关闭,但它们将返回池中。这需要完成,否则连接池将认为正在使用连接,当您达到池限制时,将不会打开新连接。 将会话工厂保持为单例。您应该为每个线程打开一个新会话。
答案 1 :(得分:0)
Employee.java
使用getter和setter
hibernante.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">oracle</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
employee.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.javatpoint.mypackage.Employee" table="emp1000">
<id name="id">
<generator class="assigned"></generator>
</id>
<property name="firstName"></property>
<property name="lastName"></property>
</class>
</hibernate-mapping>
创建检索或存储对象的类 在这个类中,我们只是将employee对象存储到数据库中。
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class StoreData {
public static void main(String[] args) {
//creating configuration object
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file
//creating seession factory object
SessionFactory factory=cfg.buildSessionFactory();
//creating session object
Session session=factory.openSession();
//creating transaction object
Transaction t=session.beginTransaction();
Employee e1=new Employee();
e1.setId(115);
e1.setFirstName("sonoo");
e1.setLastName("jaiswal");
session.persist(e1);//persisting the object
t.commit();//transaction is commited
session.close();
System.out.println("successfully saved");
}
}