I'm using Spring 3.1.2 and Hibernate 4.1.7 for my web application. I want to now configure both of these. I have my hibernate.cfg.xml
file:
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!--
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
-->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
My webapp-servlet.xml
spring config file:
<beans>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation">
<value>
classpath:hibernate.cfg.xml
</value>
</property>
<property name = "dataSource" ref = "dataSource"></property>
</bean>
<bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource">
<property name = "driverClassName" value = "com.mysql.jdbc.Driver" />
<property name = "url" value = "jdbc:mysql://localhost:3306/test" />
<property name = "username" value = "root" />
<property name = "password" value = "root" />
<property name = "maxActive" value = "10" />
</bean>
</beans>
DataSource
s I can use?You don't need both of them. You can either get rid of hibernate.cfg.xml
and configure everything in LocalSessionFactoryBean
, or reuse your existing hibernate.cfg.xml
as is (in this case you don't need to configure DataSource
in Spring config).
You have the following options:
Use embedded database - it's good for testing and learning purposes
Use DriverManagerDataSource
- it's a simple non-pooled datasource that can be used for testing, etc (not recommended for production use)
Use connection pool such as DBCP or c3p0
If you deploy to application server you can use connection pool provided by the application server using JNDI
Your current configuration is sufficient, but it lacks support of Spring transaction management. In order to enable it you need to
Declare HibernateTransactionManager
Add <tx:annotation-driven>
to enable declarative transaction management (@Transactional
)
Declare TransactionTemplate
if you want to use programmatic transaction management (use it to overcome limitations of declarative transaction management)
Also don't forget to remove transaction-related properties from Hibernate configuration since they may conflict with Spring transaction management
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With