Accès MySql via Spring et jndi, tomcat

1.Configuration

Placer le jar de mysql dans le répertoire lib de tomcat

2.Dossier META-INF, context.xml de l’application

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/myApp">
    <!-- Specify a JDBC datasource -->
    <Resource name="jdbc/dbmyapp" auth="Container" type="javax.sql.DataSource"
        username="user" password="password" driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/database?autoReconnect=true"
        validationQuery="select 1" maxActive="10" maxIdle="4" />
     
   <!-- To work with the authentification -->
   <Realm className="org.apache.catalina.realm.DataSourceRealm"
    dataSourceName="jdbc/database"
    localDataSource="true"
    roleNameCol="LB_ROLENAME"
    userCredCol="LB_PASSWORD"
    userNameCol="LB_USERNAME"
    userRoleTable="T_ROLE"
    userTable="T_USER"/>
</Context>

3. applicationContext.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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:flow="http://www.springframework.org/schema/webflow-config"
       xmlns:jms="http://www.springframework.org/schema/jms"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:osgi="http://www.springframework.org/schema/osgi"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd
       http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-2.5.xsd
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
       http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
       http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
    <!--bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" />
 
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"
          p:driverClassName="${jdbc.driverClassName}"
          p:url="${jdbc.url}"
          p:username="${jdbc.username}"
          p:password="${jdbc.password}" /-->
 
 
    <!-- JPA -->
 
    <tx:annotation-driven />
     
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" /> <!-- Prints used SQL to stdout -->
                <property name="generateDdl" value="true" /> <!-- Generates tables. -->            
               <!-- <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />-->
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLInnoDBDialect" />
            </bean>
        </property>
    </bean>
    <!--
    <bean id="dataSource" class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource" destroy-method="close">
        <property name="url" value="jdbc:mysql://localhost:3306/database" />
        <property name="user" value="username" />
        <property name="password" value="123456" />
    </bean>
    -->
      <!-- ###### JNDI Lookup ###### -->
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
        <property name="jndiName" value="java:comp/env/jdbc/dbmyapp" />
    </bean>
 
 
 
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    <property name="dataSource" ref="dataSource"/>
  </bean>
 
 
   
    <context:annotation-config/>
    <bean id="basicDao" class="ch.devit.dao.service.BasicDao" />
 
 
    <bean id="surveyService" class="ch.devit.dao.service.SurveyService"
    p:basicDao-ref="basicDao"/>
 
    <bean id="surveyController" class="ch.devit.controller.SurveyController"
    p:userService-ref="surveyService" scope="prototype" />
     
</beans>