欢迎光临
我们一直在努力

程序员Spring @Transactional方法中没有事务开始 – java

程序员面试经验

使用Spring(3.0.5),Hibernate(3.6.0)和Wicket(1.4.14)开发应用程序时遇到了一个奇怪的问题。问题是:我无法将任何对象保存或修改到数据库中。 “不能”是指对象的所有更改或对EntityManager.persist(foo)的调用都被简单,无声地忽略。选择工作。

示例案例很简单-在某些检票页面上,我尝试将对象保存到数据库中,如下所示

public class ComicDetailsPage extends PublicBasePage {  @Override protected void onConfigure() {     System.out.println("In onConfigure");     super.onConfigure();     comicDAO.insert("abc");  }  @SpringBean(name="comicDAO") private ComicDAO comicDAO;      (....) 

这是comicDAO

@Service public class ComicDAO {  @PersistenceContext private EntityManager em;  (...)  @Transactional public void insert(String title) {     Comic c = new Comic();     c.setTitle(title);     em.persist(c); }  @Transactional public Comic add1toTitle(int pk) {     System.out.println("Beginning fetching");     Comic c = em.find(Comic.class, pk);     System.out.println("Fetched updating");     c.setTitle(c.getTitle()+"1");     System.out.println("Updated persisting");     em.persist(c);     System.out.println("Persisted returning");     return c; } 

我打开了日志记录,这是日志的相关部分(Hibernate和Spring都设置为TRACE)。我在我认为很重要的行中添加了**。

In onConfigure 01:53:19.330 [qtp2119047503-15] DEBUG o.s.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'txManager' **01:53:19.330 [qtp2119047503-15] DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 13006687993** **01:53:19.330 [qtp2119047503-15] DEBUG org.hibernate.transaction.JDBCTransaction - begin** 01:53:19.330 [qtp2119047503-15] DEBUG org.hibernate.jdbc.ConnectionManager - opening JDBC connection 01:53:19.335 [qtp2119047503-15] DEBUG org.hibernate.transaction.JDBCTransaction - current autocommit status: true 01:53:19.335 [qtp2119047503-15] DEBUG org.hibernate.transaction.JDBCTransaction - disabling autocommit 01:53:19.336 [qtp2119047503-15] TRACE org.hibernate.jdbc.JDBCContext - after transaction begin 01:53:19.336 [qtp2119047503-15] DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 13006687993 01:53:19.336 [qtp2119047503-15] TRACE org.hibernate.impl.SessionImpl - setting flush mode to: AUTO 01:53:19.336 [qtp2119047503-15] TRACE org.hibernate.impl.SessionImpl - setting cache mode to: NORMAL 01:53:19.337 [qtp2119047503-15] TRACE org.hibernate.engine.IdentifierValue - id unsaved-value: 0 01:53:19.337 [qtp2119047503-15] TRACE org.hibernate.event.def.AbstractSaveEventListener - transient instance of: pl.m4ks.comics.entity.Comic 01:53:19.337 [qtp2119047503-15] TRACE org.hibernate.event.def.DefaultPersistEventListener - saving transient instance **01:53:19.338 [qtp2119047503-15] TRACE org.hibernate.event.def.AbstractSaveEventListener - saving [pl.m4ks.comics.entity.Comic#<null>]** **01:53:19.341 [qtp2119047503-15] DEBUG org.hibernate.event.def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress** 01:53:19.341 [qtp2119047503-15] TRACE org.hibernate.impl.SessionImpl - closing session 01:53:19.341 [qtp2119047503-15] TRACE org.hibernate.jdbc.ConnectionManager - connection already null in cleanup : no action 01:53:19.341 [qtp2119047503-15] DEBUG org.hibernate.transaction.JDBCTransaction - commit **01:53:19.341 [qtp2119047503-15] TRACE org.hibernate.impl.SessionImpl - automatically flushing session** 01:53:19.341 [qtp2119047503-15] TRACE org.hibernate.jdbc.JDBCContext - before transaction completion 01:53:19.341 [qtp2119047503-15] TRACE org.hibernate.impl.SessionImpl - before transaction completion 01:53:19.342 [qtp2119047503-15] DEBUG org.hibernate.transaction.JDBCTransaction - re-enabling autocommit 01:53:19.342 [qtp2119047503-15] DEBUG org.hibernate.transaction.JDBCTransaction - committed JDBC Connection 01:53:19.342 [qtp2119047503-15] TRACE org.hibernate.jdbc.JDBCContext - after transaction completion 01:53:19.342 [qtp2119047503-15] DEBUG org.hibernate.jdbc.ConnectionManager - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources! 01:53:19.342 [qtp2119047503-15] TRACE org.hibernate.impl.SessionImpl - after transaction completion 01:53:19.342 [qtp2119047503-15] TRACE org.hibernate.impl.SessionImpl - closing session 01:53:19.342 [qtp2119047503-15] TRACE org.hibernate.jdbc.ConnectionManager - performing cleanup 01:53:19.342 [qtp2119047503-15] DEBUG org.hibernate.jdbc.ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)] 01:53:19.342 [qtp2119047503-15] TRACE org.hibernate.jdbc.JDBCContext - after transaction completion 01:53:19.342 [qtp2119047503-15] DEBUG org.hibernate.jdbc.ConnectionManager - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources! 01:53:19.342 [qtp2119047503-15] TRACE org.hibernate.impl.SessionImpl - after transaction completion 

当然,没有对象被保存到数据库中。

最后一个文件-我的应用程序COntext.xml

<?xml version="1.0" encoding="UTF-8"?> <beans (...)>      <context:component-scan base-package="pl.m4ks.comics"/>     <context:annotation-config />       <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">         <property name="driverClass" value="com.mysql.jdbc.Driver"/>         <property name="url" value="jdbc:mysql://localhost:8889/comics" />         <property name="username" value="root"/>         <property name="password" value="root" />               </bean>      <bean id="entityManagerFactory"           class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">         <property name="persistenceUnitName" value="main" />         <property name="dataSource" ref="dataSource" />     </bean>      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">          <property name="dataSource">             <ref bean="dataSource"/>         </property>         <property name="packagesToScan">              <value>pl.m4ks.comics</value>         </property>     </bean>       <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">         <property name="sessionFactory">             <ref bean="sessionFactory" />         </property>     </bean>      <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />      <tx:annotation-driven transaction-manager="txManager"  proxy-target-class="true"/> </beans> 

我不知道可能是什么问题以及如何解决。我不想在我的代码中管理事务-这就是Spring的目的。

参考方案

a)您正在定义一个Hibernate SessionFactory和一个JPA EntitymanagerFactory。会是什么?使用Hibernate作为提供程序的Hibernate的Session API或JPA的Entitymanager API,但不能两者都使用。

b)您已经定义了HibernateTransactionManager,但是由于您在代码中使用了EntityManager,因此需要一个JpaTransactionManager

<bean id="myTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">     <property name="entityManagerFactory" ref="myEmf"/> </bean 

这是applicationContext.xml的注释版本:

<?xml version="1.0" encoding="UTF-8"?> <beans (...)>      <context:component-scan base-package="pl.m4ks.comics"/>     <context:annotation-config />       <bean id="dataSource"      class="org.springframework.jdbc.datasource.SimpleDriverDataSource">         <property name="driverClass" value="com.mysql.jdbc.Driver"/>         <property name="url" value="jdbc:mysql://localhost:8889/comics" />         <property name="username" value="root"/>         <property name="password" value="root" />               </bean>      <!-- use either this: -->     <bean id="entityManagerFactory"     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">         <property name="persistenceUnitName" value="main" />         <property name="dataSource" ref="dataSource" />     </bean>      <!-- or this -->     <bean id="sessionFactory"     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">          <property name="dataSource">             <ref bean="dataSource"/>         </property>         <property name="packagesToScan">              <value>pl.m4ks.comics</value>         </property>     </bean>     <!-- (but not both) -->       <!-- this is correct for AnnotationSessionFactoryBean, but not if you use          LocalContainerEntityManagerFactoryBean -->      <bean id="txManager"      class="org.springframework.orm.hibernate3.HibernateTransactionManager">         <property name="sessionFactory">             <ref bean="sessionFactory" />         </property>     </bean>      <!-- not necessary, <context:annotation-config /> automatically includes this -->     <bean      class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />      <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/> </beans> 

还有一个设计说明:DAO不应是事务性的。您应该使用管理事务的服务层。请参阅this question(以及许多其他参考)以供参考。

Spring MVC拦截器映射问题 – java

我有这段XML:<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/statics/**" /> <bean class="com.company.website.servlet.StaticsHandlerIntercept…

SOAPFaultException部署在Tomcat上时,但在GlassFish中工作正常 – java

朋友们,我一直在尝试很多,阅读了很多论坛,但无法理解为什么出现此问题。我使用契约优先方法创建了一个Jax-WS WebService。创建WSDL和XSD,然后使用wsimport工具生成其余工件,为SEI提供实现。将WebService应用程序部署到Eclipse Helios中的GlassFish(Glassfish适配器和Eclipse中安装的插件)。…

页面加载而不是提交时发生struts验证 – java

请原谅我;我对Struts有点陌生。我遇到一个问题,即页面加载而不是我实际提交表单时发生了验证。我整天都在论坛上搜寻和搜寻,没有任何运气。我显然做错了一些事情,应该很容易确定,但是我还没有发现问题所在。这是我的struts.xml的片段:<action name="*Test" method="{1}" clas…

Spring Boot索引未加载 – java

我有一个Spring Boot应用程序。我没有使用@EnableWebMvc,并且我的资源在src/main/resources/static文件夹中。当我尝试加载localhost:8080/ui/时,它只是下载一个随机文件(类型:八位字节流)。如果我直接使用/ui/index.html则可以。我也在使用WebSecurityConfigurerAdapt…

DataSourceTransactionManager和JndiObjectFactoryBean和JdbcTemplate的用途是什么? – java

以下的用途是什么:org.springframework.jdbc.core.JdbcTemplate org.springframework.jdbc.datasource.DataSourceTransactionManager org.springframework.jndi.JndiObjectFactoryBean <tx:annotatio…

赞(0)
未经允许不得转载: 京大飞辅助网程序员面试-区块链javago面经 » 程序员Spring @Transactional方法中没有事务开始 – java

相关推荐

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址