Search code examples
springsessionjpaeclipselinkconnection-pooling

How do i tell EclipseLink to stop ServerSession Connection Queries


When looking at my stack trace log, i have noticed that whenever i run any queries using the entity manager or EL (JPA 2.0) provided methods em.find(), findAll() ..., there is an Injected bean from all related entites that trigger n queries depending on the relationship. I have succesfully enabled weaving with tomcat using the spring.instruments jar and i am very sure that weaving is happening and my @OneToOne, @ManyToOne lazy settings are being set and woven in time. The problem might be how i query and the ServerSession connection that executes multiple queries.

The stack trace that shows this :

[EL Fine]: sql: 2012-07-04 22:26:20.959--ServerSession(1528617164)--Connection(1657707912)--Thread(Thread[tomcat-http--9,5,main])--SELECT personID, TYPE, DATEADDED, FIRSTNAME, LASTNAME, MIDDLENAME, ACTIVE, BIRTHDAY, EMAILADDRESS, ETHNICITY, GENDER, HISPANIC, IMAGEPATH, MARITAL, NATIVELANGUAGE, PRIMARYTELEPHONE, RELIGIOUSAFFILIATION, SECONDARYTELEPHONE, version, addressID, schoolID, MAJOR, studentId FROM PERSON WHERE (personID = ?)
    bind => [101]
2012-07-04 22:26:20,963 [tomcat-http--9] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Processing injected method of bean 'org.bixin.dugsi.domain.Administrator': PersistenceElement for transient javax.persistence.EntityManager org.bixin.dugsi.domain.Person.entityManager
2012-07-04 22:26:20,964 [tomcat-http--9] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'entityManagerFactory'
2012-07-04 22:26:20,964 [tomcat-http--9] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Processing injected method of bean 'org.bixin.dugsi.domain.Administrator': PersistenceElement for transient javax.persistence.EntityManager org.bixin.dugsi.domain.Person.entityManager
2012-07-04 22:26:20,965 [tomcat-http--9] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'entityManagerFactory'
[EL Fine]: sql: 2012-07-04 22:26:20.966--ServerSession(1528617164)--Connection(264950364)--Thread(Thread[tomcat-http--9,5,main])--SELECT addressID, CITY, COUNTRY, STATE_US, STREETADDRESS, STREETADDRESS2, version, ZIPCODE FROM ADDRESS WHERE (addressID = ?)
    bind => [3]
2012-07-04 22:26:20,970 [tomcat-http--9] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Processing injected method of bean 'org.bixin.dugsi.domain.Address': PersistenceElement for transient javax.persistence.EntityManager org.bixin.dugsi.domain.Address.entityManager
2012-07-04 22:26:20,971 [tomcat-http--9] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'entityManagerFactory'
[EL Fine]: sql: 2012-07-04 22:26:20.972--ServerSession(1528617164)--Connection(255059556)--Thread(Thread[tomcat-http--9,5,main])--SELECT personID, TYPE, DATEADDED, FIRSTNAME, LASTNAME, MIDDLENAME, ACTIVE, BIRTHDAY, EMAILADDRESS, ETHNICITY, GENDER, HISPANIC, IMAGEPATH, MARITAL, NATIVELANGUAGE, PRIMARYTELEPHONE, RELIGIOUSAFFILIATION, SECONDARYTELEPHONE, version, addressID, schoolID, MAJOR, studentId FROM PERSON WHERE (addressID = ?)
    bind => [3]
2012-07-04 22:26:20,975 [tomcat-http--9] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Processing injected method of bean 'org.bixin.dugsi.domain.Student': PersistenceElement for transient javax.persistence.EntityManager org.bixin.dugsi.domain.Person.entityManager
2012-07-04 22:26:20,976 [tomcat-http--9] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'entityManagerFactory'
2012-07-04 22:26:20,976 [tomcat-http--9] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Processing injected method of bean 'org.bixin.dugsi.domain.Student': PersistenceElement for transient javax.persistence.EntityManager org.bixin.dugsi.domain.Person.entityManager
2012-07-04 22:26:20,977 [tomcat-http--9] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'entityManagerFactory'
[EL Fine]: sql: 2012-07-04 22:26:20.98--ServerSession(1528617164)--Connection(2062869828)--Thread(Thread[tomcat-http--9,5,main])--SELECT schoolID, ACTIVE, ADMISSIONSEMAILADDRESS, ADMISSIONSPHONE, CODE, description, HELPGENERALEMAILADDRESS, NAME, PRIMARYPHONE, version, addressID FROM SCHOOL WHERE (schoolID = ?)
    bind => [3]
2012-07-04 22:26:20,982 [tomcat-http--9] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Processing injected method of bean 'org.bixin.dugsi.domain.School': PersistenceElement for transient javax.persistence.EntityManager org.bixin.dugsi.domain.School.entityManager
2012-07-04 22:26:20,982 [tomcat-http--9] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'entityManagerFactory'
[EL Fine]: sql: 2012-07-04 22:26:20.983--ServerSession(1528617164)--Connection(140234280)--Thread(Thread[tomcat-http--9,5,main])--SELECT id, ACTIVE, CODE, DESCRIPTION, NAME, version, SCHOOLDEPARTMENT_schoolID FROM DEPARTMENT WHERE (SCHOOLDEPARTMENT_schoolID = ?)
    bind => [3]
[EL Fine]: sql: 2012-07-04 22:26:20.986--ServerSession(1528617164)--Connection(1812702959)--Thread(Thread[tomcat-http--9,5,main])--SELECT personID, TYPE, DATEADDED, FIRSTNAME, LASTNAME, MIDDLENAME, ACTIVE, BIRTHDAY, EMAILADDRESS, ETHNICITY, GENDER, HISPANIC, IMAGEPATH, MARITAL, NATIVELANGUAGE, PRIMARYTELEPHONE, RELIGIOUSAFFILIATION, SECONDARYTELEPHONE, version, addressID, schoolID, MAJOR, studentId FROM PERSON WHERE (schoolID = ?)
    bind => [3]

How to ignore cache and return right away after executing a query on an entity manager object?

@RooJavaBean
@RooToString
@RooJpaActiveRecord(identifierColumn = "addressID")
public class Address {


    @Id
    @TableGenerator(name = "ADDRESS_TABLE_GEN", table = "ADDRESS_SEQUENCE_TABLE", pkColumnName = "SEQ_NAME", valueColumnName = "SEQ_COUNT", pkColumnValue = "ADDR_SEQ", allocationSize = 1, initialValue = 1)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "ADDRESS_TABLE_GEN")
    @Column(name = "addressID")
    private Long id;

    @BatchFetch(BatchFetchType.JOIN)
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "address", fetch = FetchType.EAGER)
    private Set<Person> persons = new HashSet<Person>();

Applicant entity

public class Applicant extends Person {

    private String major;

    @BatchFetch(BatchFetchType.JOIN)
    @OneToMany(targetEntity = ApplicantSchool.class, cascade = { javax.persistence.CascadeType.PERSIST, javax.persistence.CascadeType.REMOVE }, mappedBy = "applicant", fetch = FetchType.EAGER)
    private Set<ApplicantSchool> schools = new HashSet<ApplicantSchool>();

}

Stack Trace; Reverting a particular OneToOne mapping

[TomcatInstrumentableClassLoader@747917a] error aspect 'org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect' woven into 'org.bixin.dugsi.domain.Registration_Roo_Jpa_ActiveRecord' must be defined to the weaver (placed on the aspectpath, or defined in an aop.xml file if using LTW).
[TomcatInstrumentableClassLoader@747917a] error aspect 'org.springframework.mock.staticmock.AnnotationDrivenStaticEntityMockingControl' woven into 'org.bixin.dugsi.domain.Registration_Roo_Jpa_ActiveRecord' must be defined to the weaver (placed on the aspectpath, or defined in an aop.xml file if using LTW).
2012-07-06 17:08:34,910 ["http-bio-8080"-exec-1] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Found injected element on class [org.bixin.dugsi.domain.Registration]: PersistenceElement for transient javax.persistence.EntityManager org.bixin.dugsi.domain.Registration.entityManager
2012-07-06 17:08:34,910 ["http-bio-8080"-exec-1] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Processing injected method of bean 'org.bixin.dugsi.domain.Registration': PersistenceElement for transient javax.persistence.EntityManager org.bixin.dugsi.domain.Registration.entityManager
2012-07-06 17:08:34,911 ["http-bio-8080"-exec-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'entityManagerFactory'
2012-07-06 17:08:34,914 ["http-bio-8080"-exec-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor#0'
[EL Warning]: metadata: 2012-07-06 17:08:34.968--ServerSession(1433743869)--Thread(Thread["http-bio-8080"-exec-1,5,main])--Reverting the lazy setting on the OneToOne or ManyToOne attribute [person] for the entity class [class org.bixin.dugsi.domain.UserAccount] since weaving was not enabled or did not occur.

This is the field EL is referring to

   @OneToOne(targetEntity = Person.class, fetch = FetchType.LAZY) 
    @JoinColumn
    private Person person;

Solution

  • You are specifying FetchType.EAGER in both the relationships you've shown, so they must always be fetched when building the source objects. As mentioned, the problem seems to be reading in Person (or Applicant) which causes Address to be fetched. Chances are that the 'address' ManyToOne in Person is also marked eager. This causes Addresses to be built, and because you have a 1:M from Address->Applicant, this query must go to the database to make sure that it fetched all Applicants.

    This same senario happens on all the referenced eagerly fetched entities, and is why it is a bad idea to have everything marked eager unless it is neccessary, especially OneToMany and ManyToMany relationships. I'd recommend you change them to lazy and only fetch them as required when the relationship is accessed, or use fetch join in queries or query hints to prefetch particular relationships that will be needed.