我正在研究一個需要與數據庫進行交互的Web應用程序項目(本例中爲Oracle 12c):Hibernate有什麼用處?它太慢?開發者缺少什麼?
爲了理解和測試Hibernate的功能(因爲我是第一次使用Hibernate):我編寫了一個調用Web服務的小型Web應用程序。該Web服務的後端反過來使用hibernate從Oracle數據庫獲取數據,(它執行只讀查詢(通過CreateSQLQuery存儲過程))。
它給了以下的結果,(在那裏我的地方(的tomcat 8),以及數據庫運行Web服務是在遠程(AWS)):
520記錄獲取:21020毫秒(約21秒,以獲取)
3846記錄獲取:172383毫秒(172秒〜3分鐘)
我有幾個問題:
我想知道什麼是使用Hibernate?
2.在我的第一個練習中它太慢了,我搜索了所有的網絡,但沒有找到任何直接的答案或方向?
3.如果你是一個誰在使用休眠,然後可以請你讓我知道你爲什麼採用這種休眠,什麼業務問題解決了(比如是它誰可以只運行基本的web應用程序的初學者?)
4.請讓我知道Hibernate的開發人員缺少什麼?
參考文獻: simple-hibernate-query-returning-very-slowly
why-is-the-hibernate-query-list-slow
sql-query-within-hibernate-extremely-slow-while-very-fast-using-sql-cient
下面是我簡單的Hibernate工作方面:
查詢:
List<Model> modelList = session.createSQLQuery("SELECT * FROM TABLE)
.addEntity(modelList.class).list();
*表 - 我爲簡單改了名字,我overyall正在運行只有一個簡單的選擇查詢來獲取的是,沒有什麼複雜的,
配置文件
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@X.X.X.X:1521:orcl</property>
<property name="hibernate.connection.password">*****</property>
<property name="hibernate.connection.username">******</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto"></property>
<mapping class="x.x.x.x.xsModel" />
<mapping class="x.x.x.x.dModel" />
</session-factory> </hibernate-configuration>
冬眠的Util
public class HibernateUtil // private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory sessionFactory = buildSessionFactory();
private static final Logger logger = LoggerFactory.getLogger(HibernateUtil.class);
private static SessionFactory buildSessionFactory()
{
try
{
if (sessionFactory == null) {
// loads configuration and mappings
Configuration configuration = new Configuration().configure()
.setProperty("hibernate.connection.driver_class", "oracle.jdbc.OracleDriver")
.setProperty("hibernate.connection.url", "jdbc:oracle:thin:@X.X.X.X:orcl")
.setProperty("hibernate.connection.password","aaaaa")
.setProperty("hibernate.connection.username","aaaaa")
.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect")
.setProperty("show_sql","true")
//.setProperty("hibernate.hbm2ddl.auto", "")
// .setProperty("hibernate.temp.use_jdbc_metadata_defaults","false")
// .setProperty("hibernate.jdbc.lob.non_contextual_creation","true")
.addAnnotatedClass(dModel.class)
.addAnnotatedClass(sModel.class);
ServiceRegistry serviceRegistry
= new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
// builds a session factory from the service registry
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
return sessionFactory;
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
logger.error("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
我已經使用JDBC並採取了採取由Hibernate ORM或多或少同一時間 - 520個記錄獲取:21020毫秒(大約21秒內提取),3846條記錄來獲取: 172383 ms(172 Seconds〜3 Minutes)(在JDBC中,第一次只增加1-2秒,第二次增加20秒)。 Native Oracle Client的含義是什麼? –