Tags in my blog

19 November 2009

Example use JPA on spring applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<context:component-scan
base-package="database.db"/>

<!-- To inject @PersistenceContext -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/database"/>
<property name="properties">
<props>
<prop key="c3p0.acquire_increment">5</prop>
<prop key="c3p0.idle_test_period">100</prop>
<prop key="c3p0.max_size">100</prop>
<prop key="c3p0.max_statements">0</prop>
<prop key="c3p0.min_size">10</prop>
<prop key="user"></prop>
<prop key="password"></prop>
</props>
</property>
</bean>

<bean id="defaultDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/database"/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>

<bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/database" />
<property name="username" value="" />
<property name="password" value="" />
<!--
Added below properties to deal with connection timeout (to db) when
pollService hasnt been used for a long time
-->
<!--
Max number of connections that can remain idle in pool, without extra
conns being released
-->
<property name="maxIdle" value="2" />
<!-- Max number of active connections in the pool -->
<property name="maxActive" value="8" />
<!--
In milliseconds - wait no longer for a connection to be returned
(when no conns available in pool)
-->
<property name="maxWait" value="10000" />
<!-- Validation Query to be used to validate connections from pool -->
<property name="validationQuery" value="SELECT 1" />
<!-- Validate objects before being returned to pool (true/false) -->
<property name="testOnBorrow" value="true" />
<!--
Below methods are deprecated in Commons DBCP 1.3 (should be removed
from here?)
-->
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="30" />
<property name="logAbandoned" value="true" />
</bean>


<!-- To enable transaction using @Transactional -->
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="databasePU"/>
<property name="dataSource" ref="c3p0DataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
</bean>
</property>
</bean>

</beans>

No comments: