0

I have a spring mvc project downloaded from the web and fully working. I'm using maven and maven tomcat plugin to manage dependencies and to run the webapp in the built-in tomcat. I'm trying to add mySql support in my project. Since i'm new to maven and maven tomcat plugin, I don't know hot to do this. Before i tried to add mysql, all was working and i was able to launch my web app simply executing a tomcat:run maven goal.

For now, when i execute tomcat:run i get a

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Here is what i've already done after some reading around the web: I added dependencies for mysql driver (and Hibernate annotations too since i want to use it) in my pom.xml, and specified the dependency for tomcat plugin:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
        <mode>context</mode>
    </configuration>
<dependencies>      
    <dependency>
        <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.9</version>
    </dependency>
</dependencies>
</plugin>                       

You can also notice a tag to specify to use a context.xml file. But I don't know where to put this file. I readed it should be generated automatically in tomcat/conf, but it's not present. So i added it manually with this content:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
     <Resource name="jdbc/mkyongdb" auth="Container" type="javax.sql.DataSource"
           maxActive="50" maxIdle="30" maxWait="10000"
           username="root" password="password" 
           driverClassName="com.mysql.jdbc.Driver"
           url="jdbc:mysql://localhost:3306/mkyongdb"/>

</Context>

Then in web.xml, located in tomcat/conf i added:

<resource-ref>
<description>MySQL Datasource example</description>
<res-ref-name>jdbc/mkyongdb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

I placed the same content in src/main/webapp/META-INF/context.xml and in src/main/webapp/WEB-INF/web.xml

With all these configuration, the error mentioned above doesn't appears. But if i try to use hibernate adding

<bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mkyongdb" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
<bean
    id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
    <property name="dataSource" >
        <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties" >
        <props>
            <prop key="hibernate.hbm2ddl.auto" >create-drop</prop>
            <prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql" >true</prop>
        </props>
    </property>
    <property name="annotatedClasses" >
        <list>
            <value>org.mose.grouporganizer.entity.AccelerometerFeatures</value>
        </list>
    </property>
</bean>

then i get the comunication link failure. What i'm missing? If it's needed i can add the full stack trace.

4

2 回答 2

0

首先升级到现在在 Apache 上的最后一个 tomcat maven 插件。

http://tomcat.apache.org/maven-plugin-2.0/

关于上下文使用

  <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <configuration>
      <contextFile>path to your context file</contextFile>
    </configuration>
  </plugin>
于 2012-11-01T07:53:27.730 回答
0

如果您的应用程序工作正常并且您想使用 MySQL 作为数据库,那么您应该在您的应用程序中添加 MySQL 驱动程序pom.xml并更改 Hibernate 配置。那它。

于 2012-10-31T13:53:18.870 回答