Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not active transaciotn: javax.persistence.TransactionRequiredException: Executing an update/delete query

why is my transaction not active? I have this message: "javax.persistence.TransactionRequiredException: Executing an update/delete query" I can't find why

aplicationContext:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c"
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/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

<context:annotation-config />
<context:component-scan base-package="com.model.dao" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/punit" />
    <property name="username" value="root" />
    <property name="password" value="" />
    <property name="maxActive" value="10" />
    <property name="maxIdle" value="10" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
        </bean>
    </property>
    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.connection.useUnicode" value="true" />
            <entry key="hibernate.connection.characterEncoding" value="UTF-8" />
            <entry key="hibernate.jdbc.batch_size" value="30" />
        </map>
    </property>
</bean>


<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
<!--        <property name="dataSource" ref="dataSource" /> -->
</bean>

DAO Impl

@Component
public class UserDaoImpl implements UserDao {

@PersistenceContext(unitName="punit")
private EntityManager em;

protected UserDaoImpl() {
}

    //other ovverrides like find, get

@Override
@Transactional
public void deleteUser(String mail, String password) {
    Query q = em.createQuery("DELETE FROM User u WHERE u.mail='"
            + mail + "'AND u.password='" + password
            + "'");
    try{
    q.executeUpdate();
    this.em.flush();
    }catch (PersistenceException pe){
        pe.printStackTrace();
    }


}

}

persistence.xml

<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence     http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit transaction-type="RESOURCE_LOCAL" name="punit">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.hbm2ddl.auto" value="create-drop" />
        <!-- e.g. validate | update | create | create-drop -->
    </properties>
</persistence-unit>
</persistence>

RemoveUser

public class RemoveUser extends BaseAction{

private static final long serialVersionUID = 1L;
private String mail;
private String password;

User user;  
@Autowired
UserDaoImpl u;

@Action(value = "removeUser", results = {
        @Result(name="success", location = "pages/loginform.jsp"),
        @Result(name="login", location = "pages/loginform.jsp")})
public String removeUser(){
    u.deleteUser(mail, password);
            //..logoutAction
    return SUCCESS;
}
//..getters and setters...
}

Error

javax.persistence.TransactionRequiredException: Executing an update/delete query

at org.hibernate.ejb.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:96)
at com.model.dao.UserDaoImpl.deleteUser(UserDaoImpl.java:73)
at com.actions.RemoveUser.removeUser(RemoveUser.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at     com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)
at     com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)
like image 826
paka Avatar asked Jun 04 '14 20:06

paka


2 Answers

I don't see a <tx:annotation-driven transaction-manager="transactionManager"/> in your xml configuration.

like image 63
Andrei Stefan Avatar answered Nov 04 '22 17:11

Andrei Stefan


Another possible cause of this error. Make certain to use the correct annotation.

In Spring, use.

org.springframework.transaction.annotation.Transactional

not

javax.transaction.Transactional.

This has bit me numerous times in unit tests.

like image 44
Paul Nehrbass Avatar answered Nov 04 '22 16:11

Paul Nehrbass