我正在尝试建立一个新的网站,但我不知道为什么会出现这个错误(我使用JUnit测试它)。我尝试了另一个项目,但我又遇到了同样的错误: 我用其他错误更新堆栈跟踪任何正文可以帮助我???它接近5天,我有这个问题,不知道如何解决它
org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction was marked for rollback only; cannot commit
at org.springframework.orm.hibernate5.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:613)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:746)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:714)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:532)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:304)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy31.add(Unknown Source)
at Hpk.onlineShopping_backend.test.categotyTest.testAddCategory(categotyTest.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: org.hibernate.TransactionException: Transaction was marked for rollback only; cannot commit
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:217)
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68)
at org.springframework.orm.hibernate5.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:609)
... 33 more
hibernate config:
@Configuration
@ComponentScan(basePackages={"Hpk.onlineShopping_backend.dto"})
@EnableTransactionManagement
public class HibernateConfig {
// Change the below based on the DBMS you choose
private final static String DATABASE_URL = "jdbc:h2:tcp://localhost/~/onlineshopping";
private final static String DATABASE_DRIVER = "org.h2.Driver";
private final static String DATABASE_DIALECT = "org.hibernate.dialect.H2Dialect";
private final static String DATABASE_USERNAME = "sa";
private final static String DATABASE_PASSWORD = "";
// dataSource bean will be available
@Bean("dataSource")
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
// Providing the database connection information
dataSource.setDriverClassName(DATABASE_DRIVER);
dataSource.setUrl(DATABASE_URL);
dataSource.setUsername(DATABASE_USERNAME);
dataSource.setPassword(DATABASE_PASSWORD);
return dataSource;
}
// sessionFactory bean will be available
@Bean
public SessionFactory getSessionFactory(DataSource dataSource) {
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource);
builder.addProperties(getHibernateProperties());
builder.scanPackages("Hpk.onlineShopping_backend.dto");
return builder.buildSessionFactory();
}
// All the hibernate properties will be returned in this method
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", DATABASE_DIALECT);
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.format_sql", "true");
//properties.put("hibernate.hbm2ddl.auto", "create");
return properties;
}
// transactionManager bean
@Bean
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
return transactionManager;
}
DTO课程:
package Hpk.onlineShopping_backend.dto;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Category {
/*
* private field
*
*/
@Id
@GeneratedValue(strategy =GenerationType.IDENTITY)
private int id;
private String name;
private String discription;
@Column(name="image_url")
private String imgUrl;
@Column(name="is_active")
private boolean active = true;
/*
* constructor
*
*/
public Category(int id, String name, String discription, String imgUrl, boolean active) {
this.id = id;
this.name = name;
this.discription = discription;
this.imgUrl = imgUrl;
this.active = active;
}
public Category() {
}
/*
* setter and getter
*
*/
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDiscription() {
return discription;
}
public void setDiscription(String discription) {
this.discription = discription;
}
public String getImgUrl() {
return imgUrl;
}
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
DAO课程:
package Hpk.onlineShopping_backend.daoImple;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import Hpk.onlineShopping_backend.dao.CategoryDAO;
import Hpk.onlineShopping_backend.dto.Category;
@Repository("categoryDAO")
public class CategoryDAOImpl implements CategoryDAO {
@Autowired
private SessionFactory sessionFactory;
private static List<Category> categories = new ArrayList<Category>();
static {
// first category
Category category = new Category();
categories.add(category);
// second category
category = new Category(2, "moblie", "discriptoin for mobile", "cat_2.png", true);
categories.add(category);
// third category
category = new Category(3, "labtop", "discriptoin for labtop", "cat_3.png", true);
categories.add(category);
}
public List<Category> list() {
return categories;
}
public Category get(int id) {
for (Category category : categories) {
if (category.getId() == id)
return category;
}
return null;
}
@Transactional
public boolean add(Category category) {
try {
sessionFactory.getCurrentSession().persist(category);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
测试类:
package Hpk.onlineShopping_backend.test;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import Hpk.onlineShopping_backend.dao.CategoryDAO;
import Hpk.onlineShopping_backend.dto.Category;
public class categotyTest {
private static AnnotationConfigApplicationContext context;
private static CategoryDAO categoryDAO;
private Category category;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
context = new AnnotationConfigApplicationContext();
context.scan("Hpk.onlineShopping_backend");
context.refresh();
categoryDAO = (CategoryDAO) context.getBean("categoryDAO");
}
@Test
public void testAddCategory() {
category = new Category();
category.setId(2);
category.setName("television");
category.setDiscription("discriptoin for tv");
category.setImgUrl("cat_1.png");
assertEquals( true,categoryDAO.add(category));
}
答案 0 :(得分:0)
noRollbackFor不应该用于数据库异常,因为数据库和hibernate会话可能不同步。即使第一个服务已回滚,此属性也应该用于业务逻辑,例如继续第二个服务。