【问题标题】:Exception javax.transaction.RollbackException: Transaction marked for rollback with GlassFish 4 - no info in the stack trace异常 javax.transaction.RollbackException:使用 GlassFish 4 标记回滚的事务 - 堆栈跟踪中没有信息
【发布时间】:2015-12-17 15:25:56
【问题描述】:

我正在使用 GlassFish 4、Spring 和 PostgreSQL。我想将实体保存到数据库中,但尝试时出现异常:

javax.transaction.RollbackException:事务标记为回滚。 在 com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:445) 在 com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.commit(JavaEETransactionManagerSimplified.java:854) 在 com.sun.enterprise.transaction.UserTransactionImpl.commit(UserTransactionImpl.java:212) 在 org.springframework.transaction.jta.JtaTransactionManager.doCommit(JtaTransactionManager.java:1021) 在 org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761) 在 org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730) 在 org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:485) 在 org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291) 在 org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) 在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 在 org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653) 在 com.amleto.server.services.controllers.AmletoController$$EnhancerBySpringCGLIB$$6d3cf30.facebookDebug() 在 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:601) 在 org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) 在 org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) 在 org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111) 在 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806) 在 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729) 在 org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) 在 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) 在 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) 在 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) 在 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:687) 在 org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:790) 在 org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) 在 org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85) 在 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) 在 org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) 在 org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) 在 com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) 在 org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282) 在 com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459) 在 com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167) 在 org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201) 在 org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175) 在 org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235) 在 org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) 在 org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284) 在 org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201) 在 org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133) 在 org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112) 在 org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) 在 org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561) 在 org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) 在 org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117) 在 org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56) 在 org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137) 在 org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565) 在 org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545) 在 java.lang.Thread.run(Thread.java:722)

其实这对我来说毫无意义。我什至不知道在哪里看,因为该消息不是很具有描述性。您知道如何检索有关此异常的更多信息吗?或者调用它的原因是什么?

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?><persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="amleto-server-model" transaction-type="JTA">
    <jta-data-source>jdbc/amleto</jta-data-source>
    <class>com.amleto.server.model.entities.FacebookDebug</class>
    <properties>
        <property name="openjpa.jdbc.Schema" value="public"/>
        <property name="openjpa.TransactionMode" value="managed" />
        <property name="openjpa.ConnectionFactoryMode" value="managed" />
        <property name="openjpa.jdbc.DBDictionary" value="postgres" />
        <!-- Log all queries performed against the database. -->
        <!-- Do not use in production, this will generate a lot of output. -->
        <property name="openjpa.Log" value="SQL=TRACE"/>
    </properties>
</persistence-unit>

AmletoController.java:

package com.amleto.server.services.controllers;

import java.sql.Timestamp;
import java.util.GregorianCalendar;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.amleto.server.model.entities.FacebookDebug;
import com.amleto.server.services.utils.SharedEntityManager;

@Controller
public class AmletoController {

    @Autowired
    SharedEntityManager entityManager;

    @Transactional
    @ResponseBody
    @RequestMapping(value = "/facebookDebug", method=RequestMethod.GET)
    public String facebookDebug(@RequestParam(value="action", required=true) String action, 
                                @RequestParam(value="userId", required=true) String userId) {

            FacebookDebug fb = new FacebookDebug();
            fb.setAction(action);
            fb.setUserId(userId);
            GregorianCalendar dateCreate = new GregorianCalendar();
            fb.setDateCreate(new Timestamp(dateCreate.getTimeInMillis()));
            entityManager.getEntityManager().persist(fb);

            /*
            int returnValue = entityManager.getEntityManager().createNativeQuery("insert into facebook_debug (action, user_id, date_create) values(?,?,current_timestamp)")
                    .setParameter("1", action)
                    .setParameter("2", userId)
                    .executeUpdate();       
            */

            return "why it doesn't work?";
    }
}

FacebookDebug.java:

package com.amleto.server.model.entities;

import java.io.Serializable;
import javax.persistence.*;
import java.sql.Timestamp;


/**
 * The persistent class for the facebook_debug database table.
 * 
 */
@Entity
@Table(name="facebook_debug")
@NamedQuery(name="FacebookDebug.findAll", query="SELECT f FROM FacebookDebug f")
public class FacebookDebug implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="facebook_debug_id")
    private Integer facebookDebugId;

    private String action;

    @Column(name="date_create")
    private Timestamp dateCreate;

    @Column(name="user_id")
    private String userId;

    public FacebookDebug() {
    }

    public Integer getFacebookDebugId() {
        return this.facebookDebugId;
    }

    public void setFacebookDebugId(Integer facebookDebugId) {
        this.facebookDebugId = facebookDebugId;
    }

    public String getAction() {
        return this.action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public Timestamp getDateCreate() {
        return this.dateCreate;
    }

    public void setDateCreate(Timestamp dateCreate) {
        this.dateCreate = dateCreate;
    }

    public String getUserId() {
        return this.userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

}

SharedEntityManager.java:

package com.amleto.server.services.utils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SharedEntityManager implements ApplicationContextAware {
    private static ApplicationContext _appCtx;

    @PersistenceContext(name="amleto-server-model")
    private EntityManager em;

    private static SharedEntityManager instance;

    public static SharedEntityManager getInstance() {
        if(instance == null) {
            instance = new SharedEntityManager();
        }
        return instance;
    }

    public EntityManager getEntityManager() {
        if(this.em == null) {
            this.em = _appCtx.getBean(EntityManager.class);
        }
        return this.em;
    }

    @Override
    public void setApplicationContext(ApplicationContext appCtx) throws BeansException {
        _appCtx = appCtx;
    }
}

服务器上的 JDBC 连接池似乎工作正常(ping 成功)。我什至可以使用此连接从项目中的表中生成 JPA 实体。 PS。注释代码(本机查询)产生此错误:

org.postgresql.util.PSQLException:错误:关系“facebook_debug”不存在

但是这个实体是存在的。我数据库中的所有名称都是小写的,实体映射看起来不错。可能架构选择存在问题,但同样,在配置中看起来还不错。

将此属性添加到persistence.xml后:

<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>

我回到上面的堆栈跟踪中刚刚链接的事务回滚异常。在向 GlassFish 日志记录配置添加其他配置后,我在异常之前获得了以下堆栈跟踪:

[2015-09-21T18:36:15.256+0200] [glassfish 4.1] [配置] [] [org.eclipse.persistence.session.file:/C:/glassfish4/glassfish/domains/domain2/eclipseApps/amleto-server-ear/amleto-server-services-ws_war/WEB-INF/lib/amleto-server-model -0.1.0-SNAPSHOT.jar_amleto-server-model.connection] [tid: _ThreadID=27 _ThreadName=http-listener-1(2)] [timeMillis: 1442853375256] [levelValue:700] [[连接(数据库登录( platform=>PostgreSQLPlatform 用户名=> "" 连接器=>JNDIConnector 数据源名称=>null ))]]

[2015-09-21T18:36:15.261+0200] [glassfish 4.1] [配置] [] [org.eclipse.persistence.session.file:/C:/glassfish4/glassfish/domains/domain2/eclipseApps/amleto-server-ear/amleto-server-services-ws_war/WEB-INF/lib/amleto-server-model -0.1.0-SNAPSHOT.jar_amleto-server-model.connection] [tid: _ThreadID=27 _ThreadName=http-listener-1(2)] [timeMillis: 1442853375261] [levelValue:700] [[已连接: JDBC:在PostgreSQL://本地主机/阿姆莱托日志级别= 0&prepareThreshold = 5&preparedStatementCacheQueries = 256&preparedStatementCacheSizeMiB = 5&defaultRowFetchSize = 0&binaryTransfer =真兼容= 9.4&readOnly的=假binaryTransferEnable =&binaryTransferDisable =&unknownLength = 2147483647&logUnclosedConnections =假disableColumnSanitiser =假TCPKEEPALIVE =假loginTimeout = 0&connectTimeout = 0&了socketTimeout = 0&receiveBufferSize = -1&sendBufferSize = -1&useSpnego=false&gsslib=auto&sspiServiceClass=POSTGRES&allowEncodingChanges=false&targetServerType=any&loadBalanceHosts=true&hostRecheckSeconds=10 用户:postgres 数据库:PostgreSQL 版本:9.4.4 驱动程序:PostgreSQL 本机驱动程序版本:PostgreSQL 9.4 JDBC4 (build 1202)]]

[2015-09-21T18:36:15.582+0200] [glassfish 4.1] [INFO] [] [org.eclipse.persistence.session.file:/C:/glassfish4/glassfish/domains/domain2/eclipseApps/amleto-server-ear/amleto-server-services-ws_war/WEB-INF/lib/amleto-server-model -0.1.0-SNAPSHOT.jar_amleto-server-model.connection] [tid: _ThreadID=27 _ThreadName=http-listener-1(2)] [timeMillis: 1442853375582] [levelValue: 800] [[ 文件:/C:/glassfish4/glassfish/domains/domain2/eclipseApps/amleto-server-ear/amleto-server-services-ws_war/WEB-INF/lib/amleto-server-model-0.1.0-SNAPSHOT.jar_amleto-服务器模型 登录成功]]

[2015-09-21T18:36:15.858+0200] [glassfish 4.1] [FINE] [] [org.eclipse.persistence.session.file:/C:/glassfish4/glassfish/domains/domain2/eclipseApps/amleto-server-ear/amleto-server-services-ws_war/WEB-INF/lib/amleto-server-model -0.1.0-SNAPSHOT.jar_amleto-server-model.sql] [tid: _ThreadID=27 _ThreadName=http-listener-1(2)] [timeMillis: 1442853375858] [levelValue: 500] [[ 插入 facebook_debug (action, user_id, date_create) values(?,?,current_timestamp) bind => [2个参数绑定]]]

【问题讨论】:

  • 请用您的 persistence.xml 和 com.amleto.server.services.controllers.AmletoController 的代码更新问题。
  • 我已经更新了我的问题。请现在看,如果我需要提供更多信息,请告诉我。

标签: java spring postgresql transactions glassfish


【解决方案1】:

我猜这个问题是由无法找到的表引起的,但我目前没有看到确切的原因。

您可以尝试以下方法:

将此添加到您的persistence.xml

<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>

它应该指示 OpenJPA 创建不存在的表。也许这会告诉你,桌子应该在哪里。

更新 #1:

您可以尝试像这样打印嵌套的异常堆栈跟踪:

    try {
        entityManager.getEntityManager().persist(fb);
    } catch (Exception x) {

        if (x.getCause() != null) {
            x.getCause().printStackTrace();

            if (x.getCause().getCause() != null) {
                x.getCause().getCause().printStackTrace();
            }
        }
    }

【讨论】:

  • 请看我的第二个回答。
  • 不幸的是,catch 块没有执行。我已经在调试模式下进行了检查。应用程序永远不会进入它。请检查我更新的问题。我在实体和 SharedEntityManager 中添加了一些代码 sn-ps。
  • 还有一件事:我在我的 persistence.xml 中使用 OpenJPA 配置,但 GlassFish 不使用 EclipseLink?也许这就是问题所在?
  • 是的,你是对的,但它似乎应该适用于 OpenJPA。看看this
  • 是的,这是我用来解决问题的线程。实际上,我必须将 OpenJPA 提供程序添加到 persistence.xml 文件,并将所需的库添加到 GlassFish。我不得不执行一些额外的步骤。我会尽快找到解决方案更新我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-11
  • 1970-01-01
  • 2013-03-02
  • 2018-12-03
  • 1970-01-01
  • 2011-01-05
  • 1970-01-01
相关资源
最近更新 更多