2017-02-18 9 views
3

Я новичок в Spring-MVC и Hibernate. Попытка создать тестовое веб-приложение с Spring-MVC (4.0.3), Hibernate (4.3.5) и с использованием MySQL в качестве задней части.Не удалось открыть сеанс Hibernate для транзакции; Вложенное исключение - org.hibernate.exception.JDBCConnectionException: Не удалось открыть соединение

Нет проблем при подключении к БД при попытке получить данные из одного и того же двоичного файла в образце testJavaClass с помощью простых операторов соединения JDBC, и я могу получить записи.

Error Log:

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> 

Структура проекта в Eclipse:

enter image description here

сервлет-context.xml: (обновлено в соответствии с ответом @Maciej Kowalski, и я до сих пор вижу ту же проблему)

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 

<!-- DispatcherServlet Context: defines this servlet's request-processing 
    infrastructure --> 

<!-- Enables the Spring MVC @Controller programming model --> 
<annotation-driven /> 

<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    up static resources in the ${webappRoot}/resources directory --> 
<resources mapping="/resources/**" location="/resources/" /> 

<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    in the /WEB-INF/views directory --> 
<beans:bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <beans:property name="prefix" value="/WEB-INF/views/" /> 
    <beans:property name="suffix" value=".jsp" /> 
</beans:bean> 

<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
    destroy-method="close"> 
    <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> 

<!-- Hibernate 4 SessionFactory Bean definition --> 
<beans:bean id="hibernate4AnnotatedSessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <beans:property name="dataSource" ref="dataSource" /> 
    <beans:property name="annotatedClasses"> 
     <beans:list> 
      <beans:value>com.journeldev.spring.model.Person</beans:value> 
     </beans:list> 
    </beans:property> 
    <beans:property name="hibernateProperties"> 
     <beans:props> 
      <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect 
      </beans:prop> 
      <beans:prop key="hibernate.show_sql">true</beans:prop> 
     </beans:props> 
    </beans:property> 
</beans:bean> 

<beans:bean id="personDAO" class="com.journeldev.spring.dao.PersonDAOImpl"> 
    <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" /> 
</beans:bean> 
<beans:bean id="personService" class="com.journeldev.spring.service.PersonServiceImpl"> 
    <beans:property name="personDAO" ref="personDAO"></beans:property> 
</beans:bean> 

<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) --> 

<context:component-scan base-package="com.journeldev.spring" /> 

<tx:annotation-driven transaction-manager="transactionManager"/> 

<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" /> 
</beans:bean> 

<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
    <!-- 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> 
</beans:beans> 

Я проверил много других потоков в Интернете и попытался изменить свой servlet-context.xml файл, но ничего не решить мою проблему.

Ex: пытался добавить дополнительные параметры в соответствии с этим StackOverflow Thread, как показано ниже

<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" /> 

Результат: вопрос все еще остается тем же:

Ex 2: пытался изменить источник данных в соответствии с этим StackOverflow Thread, как показано ниже

<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> 

результат: вопрос остается тем же

Дополнение за комментарий от @James Jithin: 19-февраля-2017

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){ 

    } 
+0

Я добавил аннотацию «Транзакционный» ко всем методам метода PersonServiceImpl, который снова аннотируется с помощью «@Service» на уровне класса. –

+0

Предлагаемое чтение: [Как создать минимальный, полный и проверенный пример] (http://stackoverflow.com/help/mcve) – wogsland

+0

Просьба поделиться вашим примером подключения Java JDBC, которое вы пытались. –

ответ

0

Это одна щеколда ошибка, которую я сделал.! Я извиняюсь. Из кода и файла конфигурации xml нет проблем.

Первоначальный план: У меня есть настройка MySql на виртуальной машине и выполняется под портом 3306. Планируется запустить приложение из экземпляра tomcat, который запущен на моем хост-компьютере. Итак, я инициировал переадресацию порта с хоста на виртуальную машину с портом 3307-> 3306, а в MYSql создан удаленный пользовательский «raju», чтобы иметь возможность подключиться с моего хоста (tomcat, работающего на хосте).

Ошибка: (из-за некоторых непонятных причин). Я решил, что приложение запускает приложение в отдельном экземпляре tomcat, который работает на моей виртуальной машине. В этом случае оба tomcat и MySQL будут запускать на том же компьютере.

1: так что нет необходимости делать ПОРТ FORWARDING я должен непосредственно использовать в порт 3306.

2: Я получил разрешение текущего удаленного пользователя («Раджу») для имеют возможность подключить локально, как

grant all privileges on rajuDB.* to 'raju'@'localhost' identified by 'XXXX'; 

Окончательный результат: Теперь я могу принести детали из database.Every вещь работает, как ожидалось.

0

Вы определили ваш personService в файле XML, но вы пытаетесь добавить транзакционные поведение к нему через аннотации. Не 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> 

удалить Также в @Transactional аннотаций из службы на всякий случай.

Пример основан на: Spring Transaction Management Article. Он содержит информацию о том, как сделать весь транзакционный уровень сервиса с помощью xml-конфигурации.

+0

Или просто удалите определение компонента 'personService' из XML. Он уже аннотирует его с помощью '@ Service' и имеет сканирование компонентных компонентов. Ему не нужно было ничего делать по его конфигурации. – Naros

+0

@Naros: Я удалил bean-элемент "personService" из xml и попытаюсь запустить. Я получаю сообщение об ошибке. Класс personController ожидает, что «personService» будет автоуведомлен. –

+0

@Maciej Kowalski: Я попробую обновить свой код в соответствии с вашими комментариями, и я опубликую здесь свои комментарии. –

0

Вы должны доверять сообщение об ошибке:

org.hibernate.exception.JDBCConnectionException: Не удалось открыть соединение

Просто потому, что вы можете подключиться к БД с помощью клиентского приложения MySql, не означает, что вы можете получить к БД через TCP. MySQL может подключаться через TCP-порт или подключаться через канал. Возможно, ваш клиент mySql подключается к базе данных с использованием именованного канала.

https://dev.mysql.com/doc/refman/5.7/en/connecting.html

+0

У меня есть MySQL WorkBench, устанавливающий соединение с БД и подключение к стандарту (TCP/IP). –

Смежные вопросы