Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法为事务打开休眠会话;嵌套异常为org.hibernate.exception.JDBCConnectionException:无法打开连接_Java_Mysql_Hibernate_Spring Mvc_Maven 3 - Fatal编程技术网

Java 无法为事务打开休眠会话;嵌套异常为org.hibernate.exception.JDBCConnectionException:无法打开连接

Java 无法为事务打开休眠会话;嵌套异常为org.hibernate.exception.JDBCConnectionException:无法打开连接,java,mysql,hibernate,spring-mvc,maven-3,Java,Mysql,Hibernate,Spring Mvc,Maven 3,我不熟悉SpringMVC和Hibernate。尝试使用SpringMVC(4.0.3)、Hibernate(4.3.5)创建测试web应用程序,并使用MySQL作为后端 连接到DB没有问题,因为我尝试使用简单的JDBC连接语句从示例testJavaClass中的同一个DB中获取数据,并且能够获取记录 错误日志: org.springframework.web.util.NestedServletException: Request processing failed; nested excep

我不熟悉SpringMVC和Hibernate。尝试使用SpringMVC(4.0.3)Hibernate(4.3.5)创建测试web应用程序,并使用MySQL作为后端

连接到DB没有问题,因为我尝试使用简单的JDBC连接语句从示例testJavaClass中的同一个DB中获取数据,并且能够获取记录

错误日志:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is  org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is   org.hibernate.exception.JDBCConnectionException: Could not open connection
 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)

root cause

 org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Could not open connection
org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:515)
org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:420)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:257)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
com.sun.proxy.$Proxy130.listPersons(Unknown Source)
com.journeldev.spring.PersonController.listPersons(PersonController.java:29)
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:215)
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:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
以下是我的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>org.raju.spring</groupId>
    <artifactId>spring-mvc-hibernate</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

  <properties>
    <java-version>1.7</java-version>
    <org.springframework-version>4.0.3.RELEASE</org.springframework-version>
    <org.aspectj-version>1.7.4</org.aspectj-version>
    <org.slf4j-version>1.7.5</org.slf4j-version>
    <hibernate.version>4.3.5.Final</hibernate.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
             </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <!-- Apache Commons DBCP -->
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>

    <!-- Spring ORM -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <!-- AspectJ -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${org.aspectj-version}</version>
    </dependency>

    <!-- Logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j-version}</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.15</version>
        <exclusions>
            <exclusion>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
            </exclusion>
            <exclusion>
                <groupId>javax.jms</groupId>
                <artifactId>jms</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxtools</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jmx</groupId>
                <artifactId>jmxri</artifactId>
            </exclusion>
        </exclusions>
        <scope>runtime</scope>
    </dependency>

    <!-- @Inject -->
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- Test -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.7</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                         <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArgument>-Xlint:all</compilerArgument>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <mainClass>org.test.int1.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
    <finalName>${project.artifactId}</finalName>
   </build>
  </project>
结果:问题依然存在:

Ex 2:尝试根据此更改数据源,如下所示


您已经在xml文件中定义了
personService
,但尝试通过注释向其添加事务行为。不是100%确定,但我会一直坚持xml定义,并在其中添加事务配置:

<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <!-- the transactional semantics... -->
        <tx:attributes>
            <!-- all methods starting with 'get' are read-only -->
            <tx:method name="get*" read-only="true"/>
            <!-- other methods use the default transaction settings (see below) -->
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!-- ensure that the above transactional advice runs for any execution
        of an operation defined by the FooService interface -->
    <aop:config>
        <aop:pointcut id="personServiceOperation" expression="execution(* com.journeldev.spring.service.PersonServiceImpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="personServiceOperation"/>
    </aop:config>

您应该信任错误消息:

org.hibernate.exception.JDBCConnectionException:无法打开 联系

仅仅因为您可以使用mySql客户端应用程序连接数据库,并不意味着您可以通过TCP访问数据库。MySQL可以通过TCP端口连接,也可以使用管道连接。您的mySql客户端可能正在使用命名管道连接到数据库


这是我犯的一个非常严重的错误。!我道歉。代码和xml配置文件没有问题

初始计划:我在虚拟机中安装了MySql,并在端口3306下运行。计划是从我的主机上运行的tomcat实例运行应用程序。所以,我已经用端口3307->3306启动了从主机到VM的端口转发,并且在MYSql中创建了一个远程用户“raju”,以便能够从我的主机(在主机中运行的tomcat)进行连接

错误:(因为一些无法解释的原因)。我已决定 在上运行的单独tomcat实例中运行应用程序 我的虚拟机。在这种情况下,tomcat和MySQL都将运行 在同一台机器上

1:所以没有必要做端口转发,我应该直接使用 端口3306

2:我已向当前远程用户(“raju”)授予访问权限 有能力以本地方式连接


最终结果:现在我可以从数据库中获取详细信息了。一切正常。

如果您发现连接代码的每一位都是正确的,那么您应该检查您的Mysql版本,该版本应该与Spring兼容。
一些较旧的版本不适用于Spring最新版本。

我已经在PersonServiceImpl方法的所有方法中添加了“事务性”注释,该方法再次用“@Service”注释在类级别。建议阅读:请共享您尝试的Java JDBC连接示例。或者从XML中删除
personService
bean定义。他已经用
@Service
对其进行了注释,并设置了组件扫描。他不必根据自己的配置做任何其他事情。@Naros:我已经从xml中删除了“personService”bean,并尝试运行它。我犯了一个错误。“personController”类要求“personService”自动连接。@Maciej Kowalski:我将尝试根据您的评论更新我的代码,并在此处发布我的评论。@Maciej Kowalski:我已删除personService的“事务性”注释,并如上所述更新了我的“servlet context.xml”。我仍然看到同样的问题。MySql数据库点是否需要任何更改。@GhostRider
基本上告诉spring找到任何带注释的
@组件或其派生类,并将它们创建为bean。这就是为什么您不需要在XML中列出它们,除非您的Spring设置有其他问题。我有MySQL WorkBench设置到DB的连接,并通过标准(TCP/IP)连接方法进行连接。我没有在aws环境中输入正确的服务器地址。这帮我弄明白我做错了什么。谢谢。我的问题解决了,我也犯了同样的错误。我建议您添加您的回复作为评论,而不是添加作为答案,因为您没有给出任何潜在的解决方案。
    <beans:property name="initialSize" value="7"/>
    <beans:property name="maxActive" value="7"/>
    <beans:property name="maxWait" value="10000"/>
    <beans:property name="validationQuery" value="SELECT 1" />
    <beans:property name="validationQueryTimeout" value="34000" />
    <beans:property name="testOnBorrow" value="true" />
<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <beans:property name="url"
        value="jdbc:mysql://localhost:3307/rajuDB" />
    <beans:property name="username" value="raju" />
    <beans:property name="password" value="raju" />
</beans:bean>
try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection  conn=DriverManager.getConnection("jdbc:mysql://localhost:3307/rajuDB", "raju", "raju");
        Statement state=conn.createStatement();
        ResultSet rs=state.executeQuery("select * from Person");

        if(rs!=null && rs.next())
        {
            System.out.println("conn connected");
        }
        else
        {
            System.out.println("not connected");
        }
    }
    catch(Exception e){

    }
<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <!-- the transactional semantics... -->
        <tx:attributes>
            <!-- all methods starting with 'get' are read-only -->
            <tx:method name="get*" read-only="true"/>
            <!-- other methods use the default transaction settings (see below) -->
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!-- ensure that the above transactional advice runs for any execution
        of an operation defined by the FooService interface -->
    <aop:config>
        <aop:pointcut id="personServiceOperation" expression="execution(* com.journeldev.spring.service.PersonServiceImpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="personServiceOperation"/>
    </aop:config>
grant all privileges on rajuDB.* to 'raju'@'localhost' identified by 'XXXX';