Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring application doesn't appear to be persisting data
    text
    copied!<p>I'm trying to write something into my database but it's not working despite it reporting "Successfully completed request". After the success everything seems to work fine and my controller redirects me correctly.</p> <p><strong>Debug</strong></p> <pre><code>DEBUG a.d.p.payment.PaymentServiceImpl - Requesting to persist new user'max_test@test.com'. DEBUG a.d.p.payment.model.PaymentDAOImpl - Persisting com.app.payment.model.PaymentUser@86ceb985. DEBUG o.s.o.j.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler - Creating new EntityManager for shared EntityManager invocation DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 13771737739 DEBUG o.h.e.def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress DEBUG o.s.o.jpa.EntityManagerFactoryUtils - Closing JPA EntityManager DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'redirectForm' DEBUG o.s.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'redirectForm'; URL [/WEB-INF/jsp/redirectForm.jsp]] in DispatcherServlet with name 'payment' DEBUG o.s.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/jsp/redirectForm.jsp] in InternalResourceView 'redirectForm' DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request </code></pre> <p><strong>appContext.xml (root context)</strong></p> <pre><code>&lt;context:annotation-config /&gt; &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt; &lt;property name="username" value="test" /&gt; &lt;property name="password" value="test" /&gt; &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt; &lt;!--payment_test is the name of the schema --&gt; &lt;property name="url" value="jdbc:mysql://test1.com:3306/payment_test" /&gt; &lt;/bean&gt; &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="persistenceUnitName" value="payment" /&gt; &lt;property name="persistenceUnitManager"&gt; &lt;bean class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager" &gt; &lt;property name="defaultDataSource" ref="dataSource" /&gt; &lt;/bean&gt; &lt;/property&gt; &lt;property name="jpaVendorAdapter"&gt; &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt; &lt;property name="showSql" value="true" /&gt; &lt;property name="generateDdl" value="true" /&gt; &lt;property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" /&gt; &lt;/bean&gt; &lt;/property&gt; &lt;/bean&gt; &lt;context:component-scan base-package="com.app.payment" /&gt; &lt;context:annotation-config /&gt; &lt;!-- Auto scan the components --&gt; &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" /&gt; &lt;tx:annotation-driven /&gt; </code></pre> <p><strong>PaymentUser</strong></p> <pre><code>@Entity @Table(name="PaymentUser") public class PaymentUser { @Id @GeneratedValue private int id; ... } </code></pre> <p><strong>PaymentService</strong></p> <pre><code>@Transactional("transactionManager") @Service() public class PaymentServiceImpl implements PaymentService { @Autowired private PaymentDAO paymentDAO; // ... service methods } </code></pre> <p><strong>Payment DAO</strong></p> <pre><code>@Repository() public class PaymentDAOImpl implements PaymentDAO { //@PersistenceContext(unitName="payment") @PersistenceContext() EntityManager em; } </code></pre> <p>It seems like it doesn't even start a transaction. Hope thats enough info for somebody to help me. Thanks for any help.</p> <p><strong>UPDATE</strong></p> <p>Getting data works fine. Persisting (EntityManager <code>em.persist()</code>) and removing ( <code>em.remove</code> ) are not working. Could there be a right problem. Meaning just read rights and no write rights, but in this case there should be an error i thought.</p> <p><strong>UPDATE 2</strong></p> <p>Added <code>&lt;aop:scoped-proxy /&gt;</code> to my dataSource bean, but no changes. Like my debug msg said</p> <pre><code>DEBUG o.h.e.def.AbstractSaveEventListener - delaying identity-insert due to no transaction in progress </code></pre> <p>There is no transaction, but where should my transaction start? </p> <p>I also checked issues <a href="https://stackoverflow.com/questions/4155991/spring-annotation-driven-transaction-manager">Spring: Annotation-driven Transaction Manager</a> but I'm not sure what to do. </p> <p>New appContext</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt; &lt;context:annotation-config /&gt; &lt;tx:annotation-driven /&gt; &lt;!-- Auto scan the components &lt;bean id="paymentDao" class="com.app.payment.model.PaymentDAOImpl" /&gt; &lt;bean id="paymentService" class="com.app.payment.PaymentServiceImpl" /&gt; should do the same --&gt; &lt;context:component-scan base-package="com.appn.payment" /&gt; &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt; &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="false" destroy-method="close"&gt; &lt;aop:scoped-proxy /&gt; &lt;property name="username" value="user" /&gt; &lt;property name="password" value="pw" /&gt; &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt; &lt;property name="url" value="jdbc:mysql://test1.com:3306/test" /&gt; &lt;!-- &lt;property name="testOnBorrow" value="true" /&gt; &lt;property name="validationQuery" value="SELECT 1" /&gt;--&gt; &lt;/bean&gt; &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt; &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt; &lt;/bean&gt; &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="persistenceUnitName" value="payment" /&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;property name="persistenceUnitManager"&gt; &lt;bean class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager" &gt; &lt;property name="defaultDataSource" ref="dataSource" /&gt; &lt;/bean&gt; &lt;/property&gt; &lt;property name="jpaVendorAdapter"&gt; &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt; &lt;property name="showSql" value="true" /&gt; &lt;property name="generateDdl" value="true" /&gt; &lt;!-- &lt;property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" /&gt; --&gt; &lt;property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" /&gt; &lt;/bean&gt; &lt;/property&gt; &lt;property name="packagesToScan" value="com.app.payment" /&gt; &lt;/bean&gt; &lt;tx:annotation-driven /&gt; &lt;/beans&gt; </code></pre> <p><strong>UPDATE 3</strong></p> <p>Tried to flush in my PaymentDAO <code>em.flush()</code> which leads me to the error message.</p> <pre><code>javax.persistence.TransactionRequiredException: no transaction is in progress at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:792) </code></pre> <p>which is:</p> <pre><code>public void flush() { if ( !isTransactionInProgress() ) { throw new TransactionRequiredException( "no transaction is in progress" ); } try { getSession().flush(); } catch ( RuntimeException e ) { throw convert( e ); } } </code></pre> <p>Do i need some kind of special session ? Also logged it in my controller </p> <pre><code>log.info("Is transaction active " + TransactionSynchronizationManager.isActualTransactionActive()); </code></pre> <p>which results to false... no sure why there is no active transaction...</p> <p><strong>UPDATE 4</strong></p> <pre><code>import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; @Transactional @Repository public class PaymentDAOImpl implements PaymentDAO { private final Logger log = LoggerFactory.getLogger(getClass()); //@PersistenceContext() @PersistenceContext(unitName="payment") EntityManager em; @Override public void persist(PaymentUser user) { log.debug("Persisting {}.", user); em.persist(user); //em.flush(); } @Override public void remove(PaymentUser user) { log.debug("Removing {}.", user); em.remove(user); } @Override public List&lt;PaymentUser&gt; getPaymentUsers() { log.debug("Fetching payment users."); return em.createQuery("FROM PaymentUser", PaymentUser.class).getResultList(); } @Override public PaymentUser getPaymentUserById(String userId) { log.debug("Fetching payment users with id '{}'.",userId); return em.createQuery( "FROM PaymentUser WHERE userId = :userId", PaymentUser.class) .setParameter("userId", userId).getSingleResult(); } @Override public void removePaymentUserById(String userId) { log.debug("Removing payment users with id '{}'.",userId); em.createQuery("DELETE FROM PaymentUser WHERE userId = :userId ", PaymentUser.class). setParameter("userId", userId).executeUpdate(); } @Override public void mergePaymentUser(PaymentUser user) { log.debug("Merging payment user '{}'.",user); em.merge(user); } } </code></pre> <p><strong>UPDATE 5</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;!-- springapp servlet --&gt; &lt;servlet&gt; &lt;servlet-name&gt;payment&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;payment&lt;/servlet-name&gt; &lt;url-pattern&gt;/payment/*&lt;/url-pattern&gt; &lt;url-pattern&gt;/paymentExternalData&lt;/url-pattern&gt; &lt;url-pattern&gt;/paymentInternalData&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;!-- Welcome files --&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;payment.jsp&lt;/welcome-file&gt; &lt;welcome-file&gt;payment.html&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; &lt;!-- S P R I N G --&gt; &lt;!-- Add Support for Spring --&gt; &lt;!-- Default applicationContext location: /WEB-INF/applicationContext.xml --&gt; &lt;!-- UTF-8 filter --&gt; &lt;filter&gt; &lt;filter-name&gt;characterEncodingFilter&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; &lt;init-param&gt; &lt;param-name&gt;encoding&lt;/param-name&gt; &lt;param-value&gt;UTF-8&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt; &lt;param-name&gt;forceEncoding&lt;/param-name&gt; &lt;param-value&gt;true&lt;/param-value&gt; &lt;/init-param&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;characterEncodingFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;/web-app&gt; </code></pre> <p><strong>UPDATE 6</strong></p> <p>payment-servlet.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt; &lt;context:annotation-config /&gt; &lt;tx:annotation-driven /&gt; &lt;!-- Auto scan the components --&gt; &lt;context:component-scan base-package="com.app.payment" /&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/jsp/"/&gt; &lt;property name="suffix" value=".jsp"/&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>appContext.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt; &lt;context:annotation-config /&gt; &lt;tx:annotation-driven /&gt; &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="false" destroy-method="close"&gt; &lt;aop:scoped-proxy /&gt; &lt;property name="username" value="test" /&gt; &lt;property name="password" value="test" /&gt; &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt; &lt;property name="url" value="jdbc:mysql://test1.com/test" /&gt; &lt;property name="testOnBorrow" value="true" /&gt; &lt;property name="validationQuery" value="SELECT 1" /&gt; &lt;/bean&gt; &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt; &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt; &lt;/bean&gt; &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="persistenceUnitName" value="payment" /&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;property name="persistenceUnitManager"&gt; &lt;bean class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager" &gt; &lt;property name="defaultDataSource" ref="dataSource" /&gt; &lt;/bean&gt; &lt;/property&gt; &lt;property name="jpaVendorAdapter"&gt; &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt; &lt;property name="showSql" value="true" /&gt; &lt;property name="generateDdl" value="true" /&gt; &lt;property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" /&gt; &lt;/bean&gt; &lt;/property&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload