如何使用Spring MVC和Hibernate在Web应用程序的运行时获取jdbc.batch_size属性的值?
时间:2020-03-06 14:28:02 来源:igfitidea点击:
根据到目前为止的发现,我可以使用以下代码:
LocalSessionFactoryBean sessionFactory = (LocalSessionFactoryBean)super.getApplicationContext().getBean("&sessionFactory"); System.out.println(sessionFactory.getConfiguration().buildSettings().getJdbcBatchSize());
但是然后我得到了一个休眠异常:
org.hibernate.HibernateException: No local DataSource found for configuration - dataSource property must be set on LocalSessionFactoryBean
有人可以照亮吗?
解决方案
尝试以下操作(由于不使用Spring,所以无法测试):
System.out.println(sessionFactory.getConfiguration().getProperty("hibernate.jdbc.batch_size"))
在我检查过的Hibernate版本上,getConfiguration不是SessionFactory的公共方法。在一些绝望的情况下,我将Session或者SessionFactory转换为其基础实现,以获取一些未公开提供的值。在这种情况下,将是:
((SessionFactoryImplementor)sessionFactory).getSettings().getJdbcBatchSize()
当然,这很危险,因为如果他们更改实现,它可能会中断。通常,我只是为了进行我无法进行的优化,然后将整个内容包装在try / catch Throwable块中,以确保它在失败时不会对任何事情造成伤害。一个更好的主意可能是在初始化Hibernate时自行设置该值,以便从一开始就知道它的含义。