java 启动jdbc 事务报错 嵌套异常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 启动jdbc 事务报错 嵌套异常相关的知识,希望对你有一定的参考价值。

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '2678A9D8A4F5FA0707A5F652001ACABE816EE484B994AE39ED963C5A4B2DA3A22B8ABA272F176A7C2C239A79A6364D41'
### The error may exist in cn/drzk/wanda/cinema/dao/form/dao/TicketsFromDao.java (best guess)
### The error may involve cn.drzk.wanda.cinema.dao.form.dao.TicketsFromDao.count
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '2678A9D8A4F5FA0707A5F652001ACABE816EE484B994AE39ED963C5A4B2DA3A22B8ABA272F176A7C2C239A79A6364D41'

这个……“Could not get JDBC Connection”和“Cannot load JDBC driver class”,很明显是找不到驱动器类。一般是因为服务器下没有相应jar,不过看那一堆乱码……有可能是解码格式的问题(没遇到过),而且是用spring加载的(?),多半是解码问题,把类名加密了。追问

是对连接进行加密了,但也进行了解密,是用的spring

追答

这个……不看代码是不知道问题在哪……虽说看了也不一定知道。
80%的可能是解密不对或在解密前就去加载类了。接下来……我无能为力……Sorry

参考技术A 是不是加载驱动那里写错了?追问

比如呢?

无法提交 Hibernate 事务;嵌套异常是 org.hibernate.Transaction 异常:JDBC 提交失败

【中文标题】无法提交 Hibernate 事务;嵌套异常是 org.hibernate.Transaction 异常:JDBC 提交失败【英文标题】:Could not commit Hibernate transaction; nested exception is org.hibernate.Transaction Exception: JDBC commit failed 【发布时间】:2013-01-09 07:47:27 【问题描述】:

当我连接到应用程序时,我在我的应用程序中使用 Windows Azure SQL Server,它显示以下错误。

org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.Transaction
Exception: JDBC commit failed
        at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:660)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393
)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.interceptor.CustomizableTraceInterceptor.invokeUnderTrace(CustomizableTraceInterceptor.java:256)
        at org.springframework.aop.interceptor.AbstractTraceInterceptor.invoke(AbstractTraceInterceptor.java:110)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)
        at com.asman.vote.mypaint.service.UserWorksService$$EnhancerByCGLIB$$803137ef.yesterdayWinner(<generated>)
        at com.asman.vote.mypaint.controller.MainController.yesterdayWinner(MainController.java:102)
        at com.asman.vote.mypaint.controller.MainController$$FastClassByCGLIB$$ff981c3a.invoke(<generated>)
        at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
        at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
        at org.springframework.aop.interceptor.CustomizableTraceInterceptor.invokeUnderTrace(CustomizableTraceInterceptor.java:256)
        at org.springframework.aop.interceptor.AbstractTraceInterceptor.invoke(AbstractTraceInterceptor.java:110)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)
        at com.asman.vote.mypaint.controller.MainController$$EnhancerByCGLIB$$c20ad12a.yesterdayWinner(<generated>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:
426)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:185)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:151)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.TransactionException: JDBC commit failed
        at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:161)
        at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
        ... 53 more
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
        at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.checkClosed(SQLServerConnection.java:388)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.commit(SQLServerConnection.java:1936)
        at org.hibernate.transaction.JDBCTransaction.commitAndResetAutoCommit(JDBCTransaction.java:170)
        at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:146)
        ... 54 more

Hibernate.cfg.xml:

........

<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="hibernate.connection.password">Asman.1234</property>`

        <property name="hibernate.connection.url">jdbc:sqlserver://mzfoe7t5fn.database.windows.net:1433;databaseName=VOTEMYPAINT;encrypt=false;packetSize=10240;loginTimeout=300</property>
        <property name="hibernate.connection.username">SQLAdmin@mzfoe7t5fn</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="hibernate.search.autoregister_listeners">false</property>
        <property name="hibernate.c3p0.min_size">5</property>

        <mapping class="com.asman.vote.mypaint.to.UserProfile" />
        <mapping class="com.asman.vote.mypaint.to.Purchasedetails" />
        <mapping class="com.asman.vote.mypaint.to.Topics" />
        <mapping class="com.asman.vote.mypaint.to.AgeGroups" />
        <mapping class="com.asman.vote.mypaint.to.Admin" />
        <mapping class="com.asman.vote.mypaint.to.TodaysTopic" />
        <mapping class="com.asman.vote.mypaint.to.UserWorks" />
        <mapping class="com.asman.vote.mypaint.to.Htmlpages" />
    </session-factory>
</hibernate-configuration>

休眠上下文.html:

<?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:p="http://www.springframework.org/schema/p" 
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">`
<context:property-placeholder location="/WEB-INF/spring.properties" />

    <!-- Enable annotation style of managing transactions -->
    <tx:annotation-driven transaction-manager="transactionManager" />   

    <!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->
    <!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html -->                           
    <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/SessionFactory.html -->
    <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/Session.html -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"

                 p:configLocation="$hibernate.config"
                 p:packagesToScan="com.asman.vote.mypaint"/>
    <!-- p:dataSource-ref="dataSource" -->
    <!-- Declare a datasource that has pooling capabilities--> <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
                destroy-method="close"
                p:driverClass="$app.jdbc.driverClassName"
                p:jdbcUrl="$app.jdbc.url"
                p:user="$app.jdbc.username"
                p:password="$app.jdbc.password"
                p:acquireIncrement="10"
                p:idleConnectionTestPeriod="60"
                p:maxPoolSize="100000"
                p:maxStatements="100"
                p:minPoolSize="100000" /> --><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
                p:sessionFactory-ref="sessionFactory" /></beans>

applicationContext.xml:

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">`

<!-- <context:property-placeholder properties-ref="deployProperties" /> -->

<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />

<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
 For example @Controller and @Service. Make sure to set the correct base-package -->
<context:component-scan base-package="com.asman.vote.mypaint" />

<!-- Configures the annotation-driven Spring MVC Controller programming model.
Note that, with Spring 3.0, this tag works in Servlet MVC only!  -->
<mvc:annotation-driven /> 

<!-- Configure the multipart resolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
    p:maxUploadSize="100000000"/>

<mvc:resources mapping="/resources/**" location="/resources/" /> 
<mvc:interceptors>
<bean class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
    <property name="sessionFactory">
        <ref local="sessionFactory" />
    </property>
</bean></mvc:interceptors><!-- Imports logging configuration -->
<import resource="trace-context.xml"/>
<import resource="hibernate-context.xml" /> 
<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"
    p:location="/WEB-INF/spring.properties" /></beans> 

spring-servlet.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:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <!-- Declare a view resolver -->
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
            p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="1"/>

</beans>

trace-context.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:p="http://www.springframework.org/schema/p" 
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">`

    <!-- For parsing classes with @Aspect annotation -->
    <aop:aspectj-autoproxy/>

    <bean id="customizableTraceInterceptor" class="com.asman.vote.mypaint.aop.TraceInterceptor"
        p:enterMessage="Entering $[targetClassShortName].$[methodName]($[arguments])"
        p:exitMessage="Leaving $[targetClassShortName].$[methodName](): $[returnValue]"/>

    <aop:config>
      <aop:advisor advice-ref="customizableTraceInterceptor" pointcut="execution(public * com.asman.vote.mypaint.service..*(..))"/>
      <aop:advisor advice-ref="customizableTraceInterceptor" pointcut="execution(public * com.asman.vote.mypaint.controller..*(..))"/>
    </aop:config>

</beans>

【问题讨论】:

跟踪表明连接已关闭。使用检查连接的连接池。 如果我在应用程序中使用了 c3po 连接池。它不起作用 跟踪显示原因:com.microsoft.sqlserver.jdbc.SQLServerException:连接在尝试检查已关闭连接时关闭。您确定 SQLServer 正在接受连接并且从 C3P0 返回的连接确实是活动连接吗? 你说when i connect to application it shows following error。如果它处于连接状态,那么为什么堆栈跟踪中有一个失败的提交?是第一次建立连接的时候吗?是在第一次提交时发生还是稍后发生?不重启就恢复了吗? 你泄露的信息太多了。我建议您下次至少删除与问题解决无关的敏感信息,例如用户名和密码。 【参考方案1】:

我也遇到过类似的错误,最简单的解决办法是用@Transactional(propagation = Propagation.REQUIRES_NEW)添加@Transaction

之后您将不会收到此错误,并且可以正常工作。

【讨论】:

【参考方案2】:

原因是您已经打开了一个休眠会话并且您正在尝试执行两个查询,但请记住第一个事务的提交不会在其他事务开始之前出现。 这可能会有所帮助。 尝试在一个事务提交或使用条件时运行其他查询。

【讨论】:

【参考方案3】:

在serviceimpl方法上方使用@Transactional

【讨论】:

【参考方案4】:

您正在使用 @Transactional,让 Spring 为您启动、提交和回滚事务,并以声明方式处理您的事务。这样做的全部意义在于不必在代码中启动、提交和回滚事务。所以方法实现应该很简单

return (List<Login>) sessionFactory.getCurrentSession().createQuery("from Login").list();

【讨论】:

以上是关于java 启动jdbc 事务报错 嵌套异常的主要内容,如果未能解决你的问题,请参考以下文章

使用 JDBC 3.0 实现对嵌套事务的支持

无法提交 Hibernate 事务;嵌套异常是 org.hibernate.Transaction 异常:JDBC 提交失败

为什么MySQL JDBC不使用“启动事务”查询来启动事务?

无法打开 JPA EntityManager 进行事务处理;嵌套异常是 org.hibernate.exception.SQLGrammarException:无法获取 JDBC 连接

EF sqlite3报错 "System.Data.Entity.Core.EntityException: 在提供程序连接上启动事务时出错。有关详细信息,请参阅内部异常。

spring开启事务时启动报错SAXParseException