Configurando datasource+pool+persistence no applicationContext

2 respostas
C

Boa tarde GUJ.

Estou tentando fazer “melhorias” nas configurações de acesso ao BD numa aplicação mas estou com dúvidas.

Algumas configurações eram feitas no persistense.xml, veja exemplo abaixo, mas ñ sei onde configurá-las no applicationContext.xml.

<property name="hibernate.connection.autoReconnect" value="true"/>
         <property name="hibernate.current_session_context_class" value="thread"/>
         <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>

Alguns property podem ser configuradas no dataSource como também no entityManagerFactory mas há outras q ñ as encontrei.

Alguém sabe orientar-me como “setar” essas variáveis (p.e.: autoReconnect) no applicationContext?

Muito obrigado.
[]'s
t++

applicationContext.xml (fragmento)

...
<context:property-placeholder location="/WEB-INF/classes/jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
   <property name="driverClass" value="${jdbc.driverClassName}"/>
   <property name="jdbcUrl" value="${jdbc.url}"/>
   <property name="user" value="${jdbc.username}"/>
   <property name="password" value="${jdbc.password}"/>
   <property name="autoCommitOnClose" value="${jdbc.autoCommitOnClose}"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
   <property name="dataSource" ref="dataSource" />
   <property name="persistenceUnitName" value="meuPU"/>
   <property name="loadTimeWeaver">
      <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
   </property>
   <property name="jpaVendorAdapter">
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
         <property name="showSql" value="${hibernate.showSql}"/>
         <property name="databasePlatform" value="${hibernate.dialect}" />
      </bean>
   </property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
   <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
...

2 Respostas

W

Eu uso assim nas minhas aplicações, e modestia a parte, gosto delas, até aceito sujestões de mudança também :stuck_out_tongue:

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence 
	version="2.0"
	xmlns="http://java.sun.com/xml/ns/persistence" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
						http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

	<persistence-unit name="mysql" />
						
</persistence>

applicationContext.xml (separo um xml por responsabilidade)

<?xml version="1.0" encoding="UTF-8"?>
<beans 
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
						http://www.springframework.org/schema/context 
						http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	
	<import resource="modules/mvc.xml" />
	<import resource="modules/database.xml"/>
	
	<context:component-scan base-package="br.com.wryel" />

</beans>

no meu database.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans 
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
						http://www.springframework.org/schema/tx 
						http://www.springframework.org/schema/tx/spring-tx.xsd">
	
	<tx:annotation-driven transaction-manager="transactionManager" />
	
	<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="dataSource" ref="mysqlDataSource" />
		<property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="false" />
                <property name="database" value="MYSQL" />  
            </bean>
        </property>
	</bean>
	
	<bean id="mysqlDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="minPoolSize" value="1" />
        <property name="maxPoolSize" value="15" />
        <property name="maxIdleTime" value="300" />
        <property name="acquireIncrement" value="3" />
        <property name="initialPoolSize" value="1" /> 
       
       	<!-- Desenvolvimento 
		
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/query?autoReconnect=true" />
		<property name="properties">
			<props>
                <prop key="user">root</prop>
                <prop key="password">r00t</prop>
                
        -->      

        
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/query?autoReconnect=true" />
		<property name="properties">
			<props>
                <prop key="user">root</prop>
                <prop key="password">r00t</prop>
				<prop key="c3p0.max_size">50</prop>
				<prop key="c3p0.min_size">1</prop>
				<prop key="c3p0.acquire_increment">3</prop>
				<prop key="c3p0.idle_test_period">300</prop>
				<prop key="c3p0.max_statements">0</prop>
				<prop key="c3p0.timeout">120</prop>
            </props>
        </property>
    </bean>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />									

</beans>

Se eu precisar por exemplo colocar o quartz na aplicação, eu crio um xml a parte, e importo ele no applicationContext.xml :slight_smile:

se não me engano, o autoReconnect você pode passar ele na url do host que nem no xml acima.

[]'s

C

:smiley:

opa, muito obrigado pelas dicas WRYEL!

[]'s
t++

Criado 2 de setembro de 2011
Ultima resposta 5 de set. de 2011
Respostas 2
Participantes 2