簡體   English   中英

如何在運行時為使用Spring MVC和Hibernate的Web應用程序獲取jdbc.batch_size屬性的值?

[英]How do I get the value of the jdbc.batch_size property at runtime for a Web application using Spring MVC and Hibernate?

根據我到目前為止發現的,我可以使用以下代碼:

LocalSessionFactoryBean sessionFactory = (LocalSessionFactoryBean)super.getApplicationContext().getBean("&sessionFactory");
    System.out.println(sessionFactory.getConfiguration().buildSettings().getJdbcBatchSize());

但后來我得到了一個Hibernate異常:

org.hibernate.HibernateException:找不到配置的本地DataSource - 必須在LocalSessionFactoryBean上設置dataSource屬性

有人可以解釋一下嗎?

嘗試以下(我不能測試它,因為我不使用Spring):

System.out.println(sessionFactory.getConfiguration().getProperty("hibernate.jdbc.batch_size"))

在我檢查過的Hibernate版本中,getConfiguration不是SessionFactory的公共方法。 在一些絕望的情況下,我將Session或SessionFactory轉換為其底層實現,以獲得一些不公開的值。 在這種情況下,將是:

((SessionFactoryImplementor)sessionFactory).getSettings().getJdbcBatchSize()

當然,這很危險,因為如果他們改變實施可能會破壞。 我通常只做這個我可以沒有的優化,然后將整個東西包裝在try / catch Throwable塊中,以確保它不會傷害任何東西,如果它失敗了。 一個更好的想法可能是在初始化Hibernate時自己設置值,這樣你就可以從一開始就知道它是什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM