How do I get the value of the jdbc.batch_size property at runtime for a Web application using Spring MVC and Hibernate?
Question
According to what I have found so far, I can use the following code:
LocalSessionFactoryBean sessionFactory = (LocalSessionFactoryBean)super.getApplicationContext().getBean("&sessionFactory"); System.out.println(sessionFactory.getConfiguration().buildSettings().getJdbcBatchSize());
but then I get a Hibernate Exception:
org.hibernate.HibernateException: No local DataSource found for configuration - dataSource property must be set on LocalSessionFactoryBean
Can somebody shed some light?
Solution
Try the following (I can't test it since I don't use Spring):
System.out.println(sessionFactory.getConfiguration().getProperty("hibernate.jdbc.batch_size"))
OTHER TIPS
On the versions of Hibernate that I've checked, getConfiguration is not a public method of SessionFactory. In a few desperate cases, I've cast a Session or SessionFactory into its underlying implementation to get at some values that weren't publicly available. In this case that would be:
((SessionFactoryImplementor)sessionFactory).getSettings().getJdbcBatchSize()
Of course, that's dangerous because it could break if they change the implementation. I usually only do this for optimizations that I can live without and then wrap the whole thing in a try/catch Throwable block just to make sure it won't hurt anything if it fails. A better idea might be to set the value yourself when you initialize Hibernate so you already know what it is from the beginning.