V
V
Valentine2016-02-19 22:48:13
Java
Valentine, 2016-02-19 22:48:13

Why doesn't Spring+Hibernate(JPA) transaction go through?

Good day everyone! I just solved one problem, so I’m suffering all day with another: a banal transaction cannot happen, if earlier, even though there were errors in the database logs, now I don’t even know how to deal with these, I will be very happy with your help:
Person.java:

import javax.persistence.*;

@Entity
@Table(name = "person")
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "name", length = 40)
    private String name;
    @Column(name = "email", length = 100)
    private String email;


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", email=" + email + "]";
    }

}

PersonDao.java:
package com.springapp.mvc.DAO;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.springapp.mvc.Entities.Person;
import org.springframework.stereotype.Repository;

@Repository("personDao")
public class PersonDao {

    @PersistenceContext
    private EntityManager entityManager;

    public EntityManager getEntityManager() {
        return entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public void insert(Person person) {        
        entityManager.merge(person);
    }

    public List<Person> selectAll() {
        Query query = entityManager.createQuery("");
        List<Person> persons = (List<Person>) query.getResultList();
        return persons;
    }
}

PersonService.java: :
import java.util.List;
import com.springapp.mvc.DAO.PersonDao;
import com.springapp.mvc.Entities.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service
public class PersonService {


    private PersonDao personDao;

    public PersonDao getPersonDao() {
        return personDao;
    }

    @Autowired
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void addPerson(Person person) {
        System.out.println("Im'here! IN Service!");
        getPersonDao().insert(person);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public List<Person> fetchAllPersons() {
        return getPersonDao().selectAll();
    }
}

mvc-dispatcher-servlet.xml: Of course, I was advised to look in the direction of liquibase, but it is necessary that at least the application with such parameters starts working.
<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:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.springapp.mvc"/>
    <mvc:annotation-driven/>
    <mvc:resources mapping="/*" location="/"/>

  
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/mydbtest?useSSL=false"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

    
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.springapp.mvc.Entities"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            </props>
        </property>
    </bean>

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

    <bean id="persistenceExceptionTranslationPostProcessor"
          class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

Tomcat Localhost Log on transaction:
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
  at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
  at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:1078)
  at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
  at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:760)
  at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2323)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  at java.lang.Thread.run(Thread.java:745)

When redeploying, the following new line appears in the server log:
WARNING [RMI TCP Connection(4)-127.0.0.1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [ROOT] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.

I thought that the problem might be in the Jdbc driver, and googled it, one of several solutions was
this
:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
19-Feb-2016 22:38:07.214 INFO [RMI TCP Connection(5)-127.0.0.1] org.apache.catalina.startup.ExpandWar.expand An expanded directory [C:\Tools\tomcat9\webapps\ROOT] was found with a last modified time that did not match the associated WAR. It will be deleted.
19-Feb-2016 22:38:07.235 SEVERE [RMI TCP Connection(5)-127.0.0.1] org.apache.catalina.startup.ContextConfig.beforeStart Exception fixing docBase for context []
 java.io.IOException: Unable to create the directory [C:\Tools\tomcat9\webapps\ROOT]
  at org.apache.catalina.startup.ExpandWar.expand(ExpandWar.java:115)

The error did not fit further, because the post is not enough
. Unfortunately, I no longer know from which end to approach this problem, I hope for your help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2016-02-20
@Waynes

In case of insert, I would advise to use persist method instead of merge

A
alexey07, 2016-02-21
@aleksey07

REQUIRED - default level, you can not specify it. Nothing else gets into the log during the transaction?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question