0

I'm trying to create global connections for multiple WAR files (apps) to use. Each project use entity manager architecture and maven. I'm using spring 3.5.1, tomcat 6.0.39, JDK 1.6.0_45, C3P0 0.9.1.2, hibernate-c3p0 4.2.3.Final, com.mchange c3p0 0.9.2

I saw examples all over but none works for me, I'm not sure why. I tried almost everything, but I have no clue what's wrong.

I've written into server.xml the following:

<Resource auth="Container"
    description="DB Connection"
    driverClass="com.mysql.jdbc.Driver"
    maxPoolSize="20"
    minPoolSize="12"
    acquireIncrement="1"
    name="jdbc/testdb1"
    user="root"
    password=""
    factory="org.apache.naming.factory.BeanFactory"
    type="com.mchange.v2.c3p0.ComboPooledDataSource"
    jdbcUrl="jdbc:mysql://localhost:3306/test_db1?autoReconnect=true" />

and into context.xml the following:

<ResourceLink name="jdbc/testdb1"
  global="jdbc/testdb1"
  type="javax.sql.DataSource" />

and into web.xml the following:

<resource-ref>
<res-ref-name>jdbc/testdb1</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>

and my project (app) specific configuration:

PART 1 (JPA XML FILE):

<bean id="transactionManagerMysql" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emfMysql"/>
</bean>
<bean id="emfMysql" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="jdbc/testdb1" />
<property name="persistenceXmlLocation" value="classpath:persistence-infrastructure.xml" />
<property name="persistenceUnitName" value="puMysql" />
<property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>        
<property name="jpaProperties">
    <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.max_fetch_depth">3</prop>
        <prop key="hibernate.jdbc.fetch_size">50</prop>
        <prop key="hibernate.jdbc.batch_size">10</prop>
        <prop key="hibernate.show_sql">true</prop>
    </props>        
</property>
</bean>
<jpa:repositories base-package="domain.data.repository"
entity-manager-factory-ref="emfMysql"
transaction-manager-ref="transactionManagerMysql"/>

PART 2 (PERSISTENCE XML FILE):

<persistence-unit name="puMysql" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>domain.Mysqltable1</class>
<properties>
    <property name="hibernate.connection.datasource" value="jdbc/testdb1"/>
</properties>
</persistence-unit>

I get an exception:

Error creating bean with name 'emfMysql' defined in class path resource [datasource-tx-jpa-infrastructure.xml]: Cannot resolve reference to bean 'jdbc/testdb1' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'jdbc/testdb1' is defined

I hope someone can help me :)

Thanks ahead

yogev
  • 3
  • 2

1 Answers1

0

It said that the datasource resource you defined in tomcat server.xml is not a bean in spring. You should define a datasource bean like this:

<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/DatabaseName"/>
</bean>

And use this bean instead of jndi name for datasource property of emfMysql.

Please refer this answer

Community
  • 1
  • 1
Mavlarn
  • 3,807
  • 2
  • 37
  • 57
  • Thank you very much! I tried for a very long time to solve this problem and that's exactly what I needed :) – yogev Feb 12 '15 at 08:37