Tag: transactions

MVC与延迟加载

纠正我,如果这是一个完全重复,我知道这个主题经常讨论,但无法找到明确的答案。 问题是: 在MVC webapp中处理Hibernate对象的最佳实用解决方案是什么? 细节: 我正在使用Hibernate,并希望尽可能利用延迟加载。 我正在使用MVC风格的webapp。 我讨厌得到延迟加载初始化exception。 我讨厌在事务之间重新连接Hibernate对象。 选项: 渴望加载一切 解决了延迟初始化问题,但使我的查询更大 使用一些“在视图中打开会话”的概念 我喜欢它的简单性 对象仍然需要重新连接,并且在AJAXy设置中,经常需要重新连接 会话将针对每个请求打开 在离开交易之前我需要“触摸”项目 看起来最脆弱……而且单调乏味 创建不同的,简化的“分离”对象,以便视图永远不会看到真正的Hibernate对象 这些可能比完整的Hibernate对象更简单,因此它不像模型的完全热切负载 我听说这个地方推荐,但似乎更多的责任/代码/工作 如果我想与Hibernate对象进行交互,请打开一个会话。 这可以很好地包含在Spring Service层中,但有时似乎过多。 例如:我想要hibernateObject.getRelatedObjects()但需要说一些像springService.getRelatedObjects(hibernateObject)东西 我错过了什么吗? 我是否过度思考过? 我有没有想过的事情? PS: 对于Web框架,我使用的是ZK,但并不真正需要ZK特定的答案。 我也使用Spring而且很酷,只有Spring特定的答案,因为它无处不在。

Spring乐观锁定:如何重试事务方法直到提交成功

我使用Spring 2.5和Hibernate JPA实现Java和“容器”管理事务。 我有一个“后用户提交”方法,它在后台更新数据,无论ConcurrencyFailureException还是StaleObjectStateExceptionexception都需要提交,因为它永远不会显示给客户端。 换句话说,需要将乐观锁定变为悲观。 (如果方法执行需要更长的时间并且有人在其他事务中更改了数据,则可能发生) 我读了很多关于幂等的东西,如果exception搜索DEFAULT_MAX_RETRIES或6.2.7则重试。 示例或第14.5章。 重试 。 我也在这里和这里找到了stackoverflow。 我试过这个: public aspect RetryOnConcurrencyExceptionAspect { private static final int DEFAULT_MAX_RETRIES = 20; private int maxRetries = DEFAULT_MAX_RETRIES; Object around(): execution( * * (..) ) && @annotation(RetryOnConcurrencyException) && @annotation(Transactional) { int numAttempts = 0; RuntimeException failureException = null; do { numAttempts++; try { return proceed(); […]

Spring @Transactional注释被忽略了

我的@Transactionnal注释似乎被忽略了。 我对Spring容器的初始化没有任何错误。 看起来我的方法还没有被Spring TX框架代理。 在执行我的服务方法期间,JDBCTemplate抛出了预期的RuntimeException。 问题是JDBC连接没有回滚并且更改是持久的。 堆栈跟踪不显示应该包装我的服务方法的任何代理符号。 编辑:添加了控制器的代码 编辑2:添加了服务的界面 这是我的服务界面。 public interface ApplicationsService { public Application getApplicationById(int id); public void createApplication(Application application); public void createInstance(Application application); public Map getUserApplications(String username); public Application newApplication(String email); } 这是我的服务。 @Service public class ApplicationsServiceImpl implements ApplicationsService { … @Transactional public void createApplication(Application application){ // Persisting the application. applicationDAO.createApplication(application); application.setId( […]

@Transactional的奇怪行为(propagation = Propagation.REQUIRES_NEW)

这是我的问题: 我正在Java EE / Spring / Hibernate应用程序上运行批处理。 该批次调用method1 。 此方法调用一个可以抛出UserException (一个扩展RuntimeException的类)。 它是这样的: @Transactional public class BatchService implements IBatchService { @Transactional(propagation=Propagation.REQUIRES_NEW) public User method2(User user) { // Processing, which can throw a RuntimeException } public void method1() { // … try { this.method2(user); } catch (UserException e) { // … } // … } } 在执行继续时捕获exception,但在事务关闭时在method1结束时抛出RollbackException。 […]

根据“尽力而为1PC”模式实现Spring ChainedTransactionManager

最好的努力1PC在这篇文章中很好地描述。 为SpringSource工作的作者David Syer提供了一个扩展Spring AbstractPlatformTransactionManager类的ChainedTransactionManager实现。 此特定事务管理器可以启动n个事务,例如JDBC或JMS会话,并使用AbstractPlatformTransactionManager n个实例执行提交/回滚,每个资源一个。 这在XA驱动程序不可用的情况下非常有用。 由于post很老,代码在Spring 2.5中运行良好,但在Spring 3+中失败了。 在Spring 3+中有一个实现类似function的请求票 。 我愿意尝试一下,但我想我先问:那里有没有实现? 谢谢

HibernateException:无法获取当前线程的事务同步会话

尝试使用我的@Service注释类时,我收到以下exception: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134) ~[spring-orm-4.1.1.RELEASE.jar:4.1.1.RELEASE] at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final] at webapp.base.repository.GenericDaoImpl.saveOrUpdate(GenericDaoImpl.java:59) ~[base-0.0.1-SNAPSHOT-classes.jar:na] at com.example.repository.PageViewDaoImpl.saveOrUpdate(PageViewDaoImpl.java:19) ~[site-0.0.1-SNAPSHOT.jar:na] at com.example.repository.PageViewDaoImpl.saveOrUpdate(PageViewDaoImpl.java:14) ~[site-0.0.1-SNAPSHOT.jar:na] at com.example.service.PageViewServiceImpl.savePageView(PageViewServiceImpl.java:26) ~[site-0.0.1-SNAPSHOT.jar:na] at com.example.interceptor.PageViewInterceptor.preHandle(PageViewInterceptor.java:29) ~[site-0.0.1-SNAPSHOT.jar:na] at org.springframework.web.servlet.HandlerExecutionChain.applyPreHandle(HandlerExecutionChain.java:130) ~[spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938) ~[spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) ~[spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) [spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) [spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:620) [servlet-api-3.0.jar:na] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) [spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) [servlet-api-3.0.jar:na] […]