像你们很多人一样,我很清楚这个例外,我已经修复了很多次。但这一次,我花了两个星期的时间去寻找和尝试,需要高层人士的帮助,没有任何成功。
我创建了一个带有面向服务架构实体/dao/service/controller的java网站。。。我使用Maven2/Hibernate/Spring(DI&AoP)和Spring MVC。数据库是MySql。
开发环境:
-窗口
-Mysql 5.6.1版
-汤姆猫7
生产环境:
-德比安
-Mysql 5.6.1版
-汤姆猫7
当我从eclipse在tomcat7中部署我的应用程序时=>没有问题一切都很好
当我在生产环境debian中部署我的应用程序时=>没有找到适合jdbc的驱动程序:mysql://127.0.0.1:3306/synapse
显然,在生产环境中,数据库可以使用与开发环境相同的用户(目前,它将在工作时更改),并具有完全的授权选项。我可以连接到数据库并与这些标识符交互。
这是我的datasource.xml

<beans xmlns="http://www.springframework.org/schema/beans"
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-2.5.xsd">

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:properties/database.properties</value>
    </property>
</bean>

<bean id="dataSource"
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

</beans>

这里是我的database.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/synapse
jdbc.username=user
jdbc.password=password

pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>fr.synapsegaming</groupId>
    <artifactId>synapse</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>
    <name>synapse</name>
    <description>Synapse gaming website and application</description>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <warSourceDirectory>src/main/webapp</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>

        <!-- JUnit testing framework -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8</version>
            <scope>test</scope>
        </dependency>

        <!-- MySQL database driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
        </dependency>

        <!-- Spring framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>3.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>3.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-hibernate</artifactId>
            <version>1.2.9</version>
        </dependency>


        <!-- Hibernate Framework -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.3.1.GA</version>
        </dependency>

        <!-- Hibernate annotation -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.3.1.GA</version>
        </dependency>

        <!-- Hibernate library dependecy start -->
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>

        <dependency>
            <groupId>antlr</groupId>
            <artifactId>antlr</artifactId>
            <version>2.7.7</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.5.2</version>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>
        <!-- Hibernate library dependecy end -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.8</version>
        </dependency>
        <!-- Hibernate data validation -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.0.1.Final</version>
        </dependency>
        <!-- HSQLDB -->
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <!-- Apache Commons FileUpload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!-- Apache Commons IO -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <!-- Java validation API -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <!-- Java Mail API -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>
</project>

堆栈
java.sql.SQLException: No suitable driver found for jdbc:mysql://127.0.0.1:3306/synapse
    java.sql.DriverManager.getConnection(DriverManager.java:596)
    java.sql.DriverManager.getConnection(DriverManager.java:187)
    org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:291)
    org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:277)
    org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:259)
    org.springframework.jdbc.datasource.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:241)
    org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
    org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:423)
    org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
    org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:139)
    org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1547)
    org.hibernate.loader.Loader.doQuery(Loader.java:673)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
    org.hibernate.loader.Loader.doList(Loader.java:2213)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
    org.hibernate.loader.Loader.list(Loader.java:2099)
    org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
    org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
    org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
    org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
    org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
    org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:811)
    fr.synapsegaming.dao.impl.PatchDaoImpl.getLastPatch(PatchDaoImpl.java:19)
    fr.synapsegaming.service.impl.PatchServiceImpl.getLastPatch(PatchServiceImpl.java:20)
    fr.synapsegaming.controller.impl.HomeControllerImpl.home(HomeControllerImpl.java:54)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我的产品tomcat位于绑定地址:127.0.0.1 pingable,telnet on 3306工作正常。
这就是我尝试过的:
-使用tomcat连接池(在CATALINA_HOME/lib中使用mysql驱动程序jar)
-安装libmysql for java
-将默认JDk设置为1.7(内置版本)
-尝试了旧版本的mysql驱动程序
-在eclipse和production中加入相同的战争(在eclipse上效果很好)
-选中tomcat绑定地址(127.0.0.1)
-选中tomcat默认类装入器路径(CATALINA_BASE/lib/*.jar)
最后但并非最不重要的一点是,当我使用jdbc连接将标准jsp测试页放在Tomcat的根目录中时,我的应用程序中一切都正常。看起来spring没有加载驱动程序(Class.forName())。有什么想法吗?

最佳答案

我终于找到了解决办法!我认为这将是我的最后一个Spring应用程序:这个框架增长得太快了。
我使用了org.springframework.jdbc.datasource.DriverManagerDataSource,它现在已被弃用。

<bean id="dataSource"
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">

为了解决这个问题,我使用了org.apache.commons.dbcp.BasicDataSource
<bean id="dataSource"
         class="org.apache.commons.dbcp.BasicDataSource">

这是Maven依赖项
<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>

享受吧!

10-05 22:38
查看更多