我在我正在开发的一个应用程序中使用了Spring和Hibernate,我在处理事务时遇到了一个问题。
我有一个服务类,它从数据库中加载一些实体,修改它们的一些值,然后(当一切都有效时)将这些更改提交给数据库。详细地说:
我得到的错误是这样的:
févr. 12, 2016 8:24:36 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
Infos: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@b7ec5d: startup date [Fri Feb 12 20:24:36 WAT 2016]; root of context hierarchy
févr. 12, 2016 8:24:36 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
Infos: Loading XML bean definitions from class path resource [application-context.xml]
févr. 12, 2016 8:24:36 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
Infos: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@9096c6: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,adrService,demandeService,rgService,utlService,myEMF,tr,org.springframework.aop.config.internalAutoProxyCreator,adrPC,utlPC,rgPC,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,org.springframework.aop.aspectj.AspectJPointcutAdvisor#3,org.springframework.aop.aspectj.AspectJPointcutAdvisor#4,org.springframework.aop.aspectj.AspectJPointcutAdvisor#5]; root of factory hierarchy
févr. 12, 2016 8:24:37 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
févr. 12, 2016 8:24:37 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.7.Final}
févr. 12, 2016 8:24:37 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
févr. 12, 2016 8:24:37 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
févr. 12, 2016 8:24:37 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
févr. 12, 2016 8:24:37 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
févr. 12, 2016 8:24:37 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: true
févr. 12, 2016 8:24:37 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@localhost:1521:xe]
févr. 12, 2016 8:24:37 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=appACTEL, password=****, autocommit=true, release_mode=auto}
févr. 12, 2016 8:24:38 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
févr. 12, 2016 8:24:38 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
févr. 12, 2016 8:24:38 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
févr. 12, 2016 8:24:38 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
févr. 12, 2016 8:24:39 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Region :REGION2
Region :REGION1
févr. 12, 2016 8:24:40 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 904, SQLState: 42000
févr. 12, 2016 8:24:40 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-00904: "REGION2": invalid identifier
Exception in thread "main" javax.persistence.RollbackException: Transaction marked as rollbackOnly
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:72)
at com.springJPA.util.TransactionAspect.commit(TransactionAspect.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:622)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:604)
at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:45)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50)
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:625)
at com.springJPA.service.RegionService$$EnhancerByCGLIB$$e6ae4f73.listeVille(<generated>)
at com.springJPA.test.Main.main(Main.java:90)
Main.java:
package com.springJPA.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.springJPA.domain.Adresse;
import com.springJPA.domain.Region;
import com.springJPA.domain.Utilisateur;
import com.springJPA.service.AdresseService;
import com.springJPA.service.RegionService;
import com.springJPA.service.UtilisateurService;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
AdresseService adrService = (AdresseService) context.getBean("adrService");
UtilisateurService utlService = (UtilisateurService) context.getBean("utlService");
RegionService rgService = (RegionService) context.getBean("rgService");
//--------
Utilisateur utilisateur1 = new Utilisateur();
utilisateur1.setNom("user1");
utilisateur1.setNomUtilisateur("user1");
utilisateur1.setMotdepasse("user1");
utlService.ajoutUtilisateur(utilisateur1);
Utilisateur utilisateur2 = new Utilisateur();
utilisateur2.setNom("user2");
utilisateur2.setNomUtilisateur("user2");
utilisateur2.setMotdepasse("user2");
utlService.ajoutUtilisateur(utilisateur2);
//--------
Adresse adresse = new Adresse();
adresse.setRue("kantawi");
adresse.setVille("Sousse");
adresse.setPays("Tunisie");
adrService.ajoutAdresse(adresse);
//--------
Region region1 = new Region();
region1.setCodep(4000);
region1.setNom_region("region1");
region1.setNum_region(1);
region1.setVille("ville1");
region1.setNum_ville(1);
rgService.ajoutRegion(region1);
//--------
Region region2 = new Region();
region2.setCodep(4001);
region2.setNom_region("region2");
region2.setNum_region(2);
region2.setVille("ville1");
region2.setNum_ville(2);
rgService.ajoutRegion(region2);
//--------
Region region3 = new Region();
region3.setCodep(4003);
region3.setNom_region("region3");
region3.setNum_region(3);
region3.setVille("ville1");
region3.setNum_ville(2);
Region region4 = new Region();
region4.setCodep(4001);
region4.setNom_region("region2");
region4.setNum_region(3);
region4.setVille("ville1");
region4.setNum_ville(2);
rgService.ajoutRegion(region4);
List<String> listeRegion= rgService.listeRegion();
for(String e:listeRegion) {
System.out.println("Region :"+e);
}
List<String> listeVille= rgService.listeVille("REGION2");
for(String e:listeVille) {
System.out.println("Ville :"+e);
}
}
}
RegionService.java:
package com.springJPA.service;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.springJPA.domain.Region;
import com.springJPA.util.MyEntityManagerFactory;
@Component("rgService")
public class RegionService {
@Autowired @Qualifier("myEMF")
private MyEntityManagerFactory emf;
private EntityManager entityManager;
public void ajoutRegion(Region region) {
entityManager = emf.getEntityManager();
entityManager.persist(region);
}
public void modifRegion(Region region) {
entityManager = emf.getEntityManager();
entityManager.merge(region);
}
/**
* supprimerRegion
* @param region
*/
public void supprimerRegion(Region region) {
entityManager = emf.getEntityManager();
entityManager.remove(region);
}
/**
* listeRegion
* @return listeRegion
*/
public List<String> listeRegion() {
entityManager = emf.getEntityManager();
TypedQuery<String> query = entityManager.createQuery("Select DISTINCT UPPER(e.nom_region) from Region e", String.class);
List<String> results = query.getResultList();
return results;
}
public List<String> listeVille(String nomRegion) {
entityManager = emf.getEntityManager();
TypedQuery<String> query = entityManager.createQuery("Select DISTINCT UPPER(e.ville) from Region e where e.nom_region =:arg1 ", String.class);
query.setParameter("arg1", nomRegion);
List<String> results = query.getResultList();
return results;
}
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
public MyEntityManagerFactory getEmf() {
return emf;
}
public void setEmf(MyEntityManagerFactory emf) {
this.emf = emf;
}
}
首先:标题中的错误信息不会告诉你哪里出了问题。它从来没有。这:
Exception in thread “main” javax.persistence.RollbackException: Transaction marked as rollbackOnly
是一个通用的错误消息,告诉您在DB操作之前或期间发生了错误,这使该事务不符合提交条件。
对于您的情况,重要的错误消息是这样的:
ERROR: ORA-00904: "REGION2": invalid identifier
这意味着您正在引用表中不存在的名为REGION2
的列。检查表中的SQL/Hibernate定义以解决此问题。
我在tomcat服务器中使用具有多个数据源配置的JTA原子事务。有时我会遇到以下异常: JTA事务意外回滚(可能是由于超时);嵌套的异常是javax。交易回滚异常:事务被设置为仅回滚 出现这种异常的原因是什么?
这很好,但并不总是在代码中抛出运行时异常。因此,我挖掘并发现如下所示的rollbackFor; 现在,我必须更改所有代码,以使用RollBackfor更改@Transactional。但是还有其他方法可以将所有@transaction advice属性更改为rollbackFor=exception.class吗?
非常感谢
我在线程“main”java中遇到异常。错误:未解析编译。 我做错了什么? 线程“main”中出现异常: java.lang.错误:未解决的编译问题: 对于类型在
因此,我在WildFly10应用服务器中配置了2个xa-datsources,我使用的是Hibernate ORM5.2.9。DB为Postgres 9.6 在我的应用程序中,我有一个EJB,它有两个嵌套的for循环,我必须在嵌套循环的每次迭代中从表中读取数据。但过了一段时间我就有了例外 javax.ejb.EJBTransactionRolledBackException:org.hiberna
我有这样的场景: 从IncomingMessage表中获取(读取和删除)记录 读取记录内容 在某些表中插入某些内容 如果在步骤1-3中出现错误(任何异常),请将错误记录插入OutgoingMessage表 否则,将成功记录插入OutgoingMessage表 因此步骤1、2、3、4应该在事务中,或者步骤1、2、3、5 但是当SqlCommandHandlerService.persist()抛出异