Tag: 事务

hibernate锁定等待超时超时;

我正在使用Hibernate,试图模拟2个并发更新到数据库中的同一行。 编辑:我移动em1.getTransaction()。提交在em1.flush()之后; 我没有得到任何StaleObjectException,这两个事务成功提交。 Session em1=Manager.sessionFactory.openSession(); Session em2=Manager.sessionFactory.openSession(); em1.getTransaction().begin(); em2.getTransaction().begin(); UserAccount c1 = (UserAccount)em1.get( UserAccount.class, “root” ); UserAccount c2 = (UserAccount)em2.get( UserAccount.class, “root” ); c1.setBalance( c1.getBalance() -1 ); em1.flush(); System.out.println(“balance1 is “+c2.getBalance()); c2.setBalance( c2.getBalance() -1 ); em2.flush(); // fail em1.getTransaction().commit(); em2.getTransaction().commit(); System.out.println(“balance2 is “+c2.getBalance()); 我在em2.flush()上得到以下exception。 为什么? 2009-12-23 21:48:37,648 WARN JDBCExceptionReporter:100 – SQL Error: 1205, SQLState: 41000 […]

Hibernate将对象保存到多个会话

我试图使用hibernate写入多个数据库。 我在单个会话对象中封装了写入和读取/写入会话。 但是,当我去保存时,我收到很多错误,表明对象已经与另一个会话相关联:“非法尝试将集合与两个打开的会话相关联” 这是我的代码: public class MultiSessionObject implements Session { private Session writeOnlySession; private Session readWriteSession; @Override public void saveOrUpdate(Object arg0) throws HibernateException { readWriteSession.saveOrUpdate(arg0); writeOnlySession.saveOrUpdate(arg0); } } 我试过驱逐物体并冲洗; 但是,这会导致“Row被另一个事务更新或删除”的问题……即使两个会话都指向不同的数据库。 public class MultiSessionObject implements Session { private Session writeOnlySession; private Session readWriteSession; @Override public void saveOrUpdate(Object arg0) throws HibernateException { readWriteSession.saveOrUpdate(arg0); readWriteSession.flush(); readWriteSession.evict(arg0); writeOnlySession.saveOrUpdate(arg0); writeOnlySession.flush(); […]

Spring @Transactional和Hibernate @LockMode注释是如何相关的

我想知道交易和锁之间的关系。 更具体地说,Spring的@Transactional与Hibernate的LockMode有什么关系。 https://docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch05.html 。 http://docs.spring.io/autorepo/docs/spring/4.2.x/spring-framework-reference/html/transaction.html 如果我在创建会话对象时没有指定任何锁定,并将@Transactional与readOnly一起使用为false ,我是否使用悲观的Concurrenct Control。 如果有人能告诉我(乐观/悲观)并发控制和交易之间的关系,那将是一个很大的帮助。 谢谢Vivek

Spring,JPA和Hibernate – 如何在没有并发问题的情况下增加计数器

我正在玩Spring和JPA / Hibernate,我对在表中递增计数器的正确方法感到困惑。 我的REST API需要根据用户操作递增和递减数据库中的某些值(在下面的示例中,喜欢或不喜欢标记会使计数器在Tag表中递增或递减1) tagRepository是一个JpaRepository (Spring-data),我已经像这样配置了事务 @Controller public class TestController { @Autowired TagService tagService public void increaseTag() { tagService.increaseTagcount(); } public void decreaseTag() { tagService.decreaseTagcount(); } } @Transactional @Service public class TagServiceImpl implements TagService { public void decreaseTagcount() { Tag tag = tagRepository.findOne(tagId); decrement(tag) } public void increaseTagcount() { Tag tag = tagRepository.findOne(tagId); increment(tag) […]

Spring事务管理器(注释样式)运行时错误

因此,当我在glassfish应用程序服务器中加载战争时,基本上我得到了一个例外。 我在我的mysql数据库中使用Spring的事务管理器。 报告的错误(完整堆栈跟踪)如下: java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0’: Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotationUtils.getAnnotation(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/Class;)Ljava/lang/annotation/Annotation; 我的applicationContext.xml文件如下: 我发现这个错误非常神秘,因为它正在抱怨的类甚至不在我的应用程序Context中。 我顺便使用Spring 3.1。 感谢您的任何建议或帮助。

在JPA中使用joinTransaction

以下代码来自JPA规范。 我无法理解为什么em.joinTransaction() createLineItem(int quantity)需要em.joinTransaction() createLineItem(int quantity) 。 任何人都可以提供合适的解释吗 @Stateful public class ShoppingCartImpl implements ShoppingCart { @PersistenceUnit private EntityManagerFactory emf; private EntityManager em; private Order order; private Product product; @PostConstruct public void init() { em = emf.createEntityManager(); } public void initOrder(Long id) { order = em.find(Order.class, id); } public void initProduct(String name) { product = (Product) […]

如何将Spring事务传播到另一个线程?

也许,我做错了什么,但我找不到适合下列情况的好方法。 我想对使用下面的Spring Batch执行作业的服务进行unit testing。 作业通过预先配置的AsyncTaskExecutor在单独的线程中执行。 在我的unit testing中,我想: 创建几个域对象并通过DAO保留它们 调用服务方法以启动作业 等到作业完成 使用DAO检索域对象并检查其状态 显然,以上所有内容都应该在一个事务中执行,但不幸的是, 事务不会传播到新线程 (我理解这背后的基本原理)。 我想到的想法: 在步骤(1)之后提交事务#1。 不好,因为在unit testing后应该回滚DB状态。 在作业配置中使用Isolation.READ_UNCOMMITTED 。 但这需要两种不同的配置用于测试和生产。

@Transactional(noRollbackFor = RuntimeException.class)不会阻止RuntimeException上的回滚

@Transactional (noRollbackFor=RuntimeException.class) public void methodA (Entity e){ service.methodB(e); } —以下服务方式— @Transactional (propagation=Propagation.REQUIRES_NEW, noRollbackFor=RuntimeException.class) public void methodB (Entity e){ dao.insert(e); } 当dao.insert(e)中的dao.insert(e)导致主键冲突并抛出一个ConstraintViolationException (它是RuntimeException的子类)时,由于我使用的noRollbackFor属性,我希望该事务仍然提交。 但是我发现外部事务(在methodA )仍然被HibernateTransactionManager和消息回滚 org.springframework.transaction.UnexpectedRollbackexception:事务已回滚,因为它已被标记为仅回滚 我发现了类似的问题,但不完全是这个问题。

unit testing中Spring Batch JobRepository的事务问题

任何人都可以帮我找出以下exception的解决方案,我想我只是不太了解事务传播机制,这阻碍了我理解下面显示的exception消息的真正含义,所以请帮助我理解整个事情,非常感谢你! java.lang.IllegalStateException: Existing transaction detected in JobRepository. Please fix this and try again (eg remove @Transactional annotations from client). at org.springframework.batch.core.repository.support.AbstractJobRepositoryFactoryBean$1.invoke(AbstractJobRepositoryFactoryBean.java:164) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at com.sun.proxy.$Proxy15.createJobExecution(Unknown Source) at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:111) at TestJob.testExcelParserTasklet(TestJob.java:36) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80) at org.testng.internal.MethodInvocationHelper$1.runTestMethod(MethodInvocationHelper.java:169) at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.run(AbstractTestNGSpringContextTests.java:158) 以下是导致上述exception的代码: public class TestJob extends BaseTest { @Test […]

如何在multithreading之间共享一个事务

我们遇到了一个适用于multithreading的场景。 在主线程中,做一些逻辑并更新数据库,在某一点上,它会调用另一个服务来更新数据库,这是在另一个线程中运行的。 我们希望两个线程共享相同的事务,这意味着,任一线程中的任一操作都失败,那么另一个线程中的操作也将被回滚。 但是工作了好几天,我发现一些post说JTA不支持multithreading。 目前我们使用Bitronix作为JTA提供程序,有没有人知道Bitronix是否支持一个Transaction中的multithreading? 或者是否有任何其他JTA提供程序支持此(独立JTA提供程序而非J2EE容器)?