Browse Source

Log warning when open-in-view is implicitly enabled for JPA or Neo4j

Closes gh-7107
pull/10822/head
Andy Wilkinson 8 years ago
parent
commit
5aa66305a8
  1. 17
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java
  2. 14
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jProperties.java
  3. 18
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java
  4. 14
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaProperties.java
  5. 12
      spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json
  6. 2
      spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
  7. 2
      spring-boot-samples/spring-boot-sample-data-jpa/src/main/resources/application.properties
  8. 1
      spring-boot-samples/spring-boot-sample-data-neo4j/src/main/resources/application.properties
  9. 5
      spring-boot-samples/spring-boot-sample-flyway/src/main/resources/application.properties
  10. 1
      spring-boot-samples/spring-boot-sample-jpa/src/main/resources/application.properties
  11. 3
      spring-boot-samples/spring-boot-sample-jta-atomikos/src/main/resources/application.properties
  12. 3
      spring-boot-samples/spring-boot-sample-jta-bitronix/src/main/resources/application.properties
  13. 3
      spring-boot-samples/spring-boot-sample-jta-jndi/src/main/resources/application.properties
  14. 3
      spring-boot-samples/spring-boot-sample-jta-narayana/src/main/resources/application.properties
  15. 1
      spring-boot-samples/spring-boot-sample-test/src/main/resources/application.properties

17
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java

@ -18,6 +18,8 @@ package org.springframework.boot.autoconfigure.data.neo4j; @@ -18,6 +18,8 @@ package org.springframework.boot.autoconfigure.data.neo4j;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.neo4j.ogm.session.SessionFactory;
import org.neo4j.ogm.session.event.EventListener;
@ -117,8 +119,23 @@ public class Neo4jDataAutoConfiguration { @@ -117,8 +119,23 @@ public class Neo4jDataAutoConfiguration {
@Configuration
protected static class Neo4jWebMvcConfiguration implements WebMvcConfigurer {
private static final Log logger = LogFactory
.getLog(Neo4jWebMvcConfiguration.class);
private final Neo4jProperties neo4jProperties;
protected Neo4jWebMvcConfiguration(Neo4jProperties neo4jProperties) {
this.neo4jProperties = neo4jProperties;
}
@Bean
public OpenSessionInViewInterceptor neo4jOpenSessionInViewInterceptor() {
if (this.neo4jProperties.getOpenInView() == null) {
logger.warn("spring.data.neo4j.open-in-view is enabled by default."
+ "Therefore, database queries may be performed during view "
+ "rendering. Explicitly configure "
+ "spring.data.neo4j.open-in-view to disable this warning");
}
return new OpenSessionInViewInterceptor();
}

14
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jProperties.java

@ -66,6 +66,12 @@ public class Neo4jProperties implements ApplicationContextAware { @@ -66,6 +66,12 @@ public class Neo4jProperties implements ApplicationContextAware {
*/
private AutoIndexMode autoIndex = AutoIndexMode.NONE;
/**
* Register OpenSessionInViewInterceptor. Binds a Neo4j Session to the thread for the
* entire processing of the request.",
*/
private Boolean openInView;
private final Embedded embedded = new Embedded();
private ClassLoader classLoader = Neo4jProperties.class.getClassLoader();
@ -102,6 +108,14 @@ public class Neo4jProperties implements ApplicationContextAware { @@ -102,6 +108,14 @@ public class Neo4jProperties implements ApplicationContextAware {
this.autoIndex = autoIndex;
}
public Boolean getOpenInView() {
return this.openInView;
}
public void setOpenInView(Boolean openInView) {
this.openInView = openInView;
}
public Embedded getEmbedded() {
return this.embedded;
}

18
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java

@ -22,6 +22,9 @@ import java.util.Map; @@ -22,6 +22,9 @@ import java.util.Map;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
@ -214,8 +217,23 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware { @@ -214,8 +217,23 @@ public abstract class JpaBaseConfiguration implements BeanFactoryAware {
@Configuration
protected static class JpaWebMvcConfiguration implements WebMvcConfigurer {
private static final Log logger = LogFactory
.getLog(JpaWebMvcConfiguration.class);
private final JpaProperties jpaProperties;
protected JpaWebMvcConfiguration(JpaProperties jpaProperties) {
this.jpaProperties = jpaProperties;
}
@Bean
public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() {
if (this.jpaProperties.getOpenInView() == null) {
logger.warn("spring.jpa.open-in-view is enabled by default. "
+ "Therefore, database queries may be performed during view "
+ "rendering. Explicitly configure "
+ "spring.jpa.open-in-view to disable this warning");
}
return new OpenEntityManagerInViewInterceptor();
}

14
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaProperties.java

@ -72,6 +72,12 @@ public class JpaProperties { @@ -72,6 +72,12 @@ public class JpaProperties {
*/
private boolean showSql = false;
/**
* Register OpenEntityManagerInViewInterceptor. Binds a JPA EntityManager to the
* thread for the entire processing of the request.
*/
private Boolean openInView;
private Hibernate hibernate = new Hibernate();
public Map<String, String> getProperties() {
@ -118,6 +124,14 @@ public class JpaProperties { @@ -118,6 +124,14 @@ public class JpaProperties {
this.showSql = showSql;
}
public Boolean getOpenInView() {
return this.openInView;
}
public void setOpenInView(Boolean openInView) {
this.openInView = openInView;
}
public Hibernate getHibernate() {
return this.hibernate;
}

12
spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

@ -145,12 +145,6 @@ @@ -145,12 +145,6 @@
"description": "Enable Mongo repositories.",
"defaultValue": true
},
{
"name": "spring.data.neo4j.open-in-view",
"type": "java.lang.Boolean",
"description": "Register OpenSessionInViewInterceptor. Binds a Neo4j Session to the thread for the entire processing of the request.",
"defaultValue": false
},
{
"name": "spring.data.neo4j.repositories.enabled",
"type": "java.lang.Boolean",
@ -227,12 +221,6 @@ @@ -227,12 +221,6 @@
"description": "MBeanServer bean name.",
"defaultValue": "mbeanServer"
},
{
"name": "spring.jpa.open-in-view",
"type": "java.lang.Boolean",
"description": "Register OpenEntityManagerInViewInterceptor. Binds a JPA EntityManager to the thread for the entire processing of the request.",
"defaultValue": true
},
{
"name": "spring.jta.enabled",
"type": "java.lang.Boolean",

2
spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

@ -3723,7 +3723,7 @@ By default, if you are running a web application, the session is bound to the th @@ -3723,7 +3723,7 @@ By default, if you are running a web application, the session is bound to the th
the entire processing of the request (i.e. the "Open Session in View" pattern). If you
don't want this behavior add the following to your `application.properties`:
[source,properties,indent=0]
----
spring.data.neo4j.open-in-view=false
----

2
spring-boot-samples/spring-boot-sample-data-jpa/src/main/resources/application.properties

@ -1,3 +1,3 @@ @@ -1,3 +1,3 @@
spring.h2.console.enabled=true
spring.jpa.open-in-view=true
logging.level.org.hibernate.SQL=debug

1
spring-boot-samples/spring-boot-sample-data-neo4j/src/main/resources/application.properties

@ -0,0 +1 @@ @@ -0,0 +1 @@
# spring.data.neo4j.open-in-view=true

5
spring-boot-samples/spring-boot-sample-flyway/src/main/resources/application.properties

@ -1,5 +1,4 @@ @@ -1,5 +1,4 @@
endpoints.flyway.web.enabled=true
spring.jpa.hibernate.ddl-auto=validate
spring.h2.console.enabled=true
spring.jpa.open-in-view=true
spring.h2.console.enabled=true

1
spring-boot-samples/spring-boot-sample-jpa/src/main/resources/application.properties

@ -0,0 +1 @@ @@ -0,0 +1 @@
spring.jpa.open-in-view=true

3
spring-boot-samples/spring-boot-sample-jta-atomikos/src/main/resources/application.properties

@ -1,2 +1,3 @@ @@ -1,2 +1,3 @@
logging.level.com.atomikos=WARN
spring.artemis.embedded.queues=accounts
spring.artemis.embedded.queues=accounts
spring.jpa.open-in-view=true

3
spring-boot-samples/spring-boot-sample-jta-bitronix/src/main/resources/application.properties

@ -1 +1,2 @@ @@ -1 +1,2 @@
spring.artemis.embedded.queues=accounts
spring.artemis.embedded.queues=accounts
spring.jpa.open-in-view=true

3
spring-boot-samples/spring-boot-sample-jta-jndi/src/main/resources/application.properties

@ -1,2 +1,3 @@ @@ -1,2 +1,3 @@
spring.jpa.generate-ddl=true
spring.datasource.jndi-name=java:jboss/datasources/bootdemo
spring.jpa.generate-ddl=true
spring.jpa.open-in-view=true

3
spring-boot-samples/spring-boot-sample-jta-narayana/src/main/resources/application.properties

@ -1,2 +1,3 @@ @@ -1,2 +1,3 @@
logging.level.com.arjuna=INFO
spring.artemis.embedded.queues=accounts
logging.level.com.arjuna=INFO
spring.jpa.open-in-view=true

1
spring-boot-samples/spring-boot-sample-test/src/main/resources/application.properties

@ -1 +1,2 @@ @@ -1 +1,2 @@
spring.datasource.url=jdbc:mysql://localhost/doesnotexist
spring.jpa.open-in-view=true

Loading…
Cancel
Save