Commit 5aa66305 authored by Andy Wilkinson's avatar Andy Wilkinson

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

Closes gh-7107
parent 95d8fe70
...@@ -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 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.SessionFactory;
import org.neo4j.ogm.session.event.EventListener; import org.neo4j.ogm.session.event.EventListener;
...@@ -117,8 +119,23 @@ public class Neo4jDataAutoConfiguration { ...@@ -117,8 +119,23 @@ public class Neo4jDataAutoConfiguration {
@Configuration @Configuration
protected static class Neo4jWebMvcConfiguration implements WebMvcConfigurer { 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 @Bean
public OpenSessionInViewInterceptor neo4jOpenSessionInViewInterceptor() { 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(); return new OpenSessionInViewInterceptor();
} }
......
...@@ -66,6 +66,12 @@ public class Neo4jProperties implements ApplicationContextAware { ...@@ -66,6 +66,12 @@ public class Neo4jProperties implements ApplicationContextAware {
*/ */
private AutoIndexMode autoIndex = AutoIndexMode.NONE; 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 final Embedded embedded = new Embedded();
private ClassLoader classLoader = Neo4jProperties.class.getClassLoader(); 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; this.autoIndex = autoIndex;
} }
public Boolean getOpenInView() {
return this.openInView;
}
public void setOpenInView(Boolean openInView) {
this.openInView = openInView;
}
public Embedded getEmbedded() { public Embedded getEmbedded() {
return this.embedded; return this.embedded;
} }
......
...@@ -22,6 +22,9 @@ import java.util.Map; ...@@ -22,6 +22,9 @@ import java.util.Map;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource; 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.BeansException;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware; 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 @Configuration
protected static class JpaWebMvcConfiguration implements WebMvcConfigurer { 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 @Bean
public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() { 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(); return new OpenEntityManagerInViewInterceptor();
} }
......
...@@ -72,6 +72,12 @@ public class JpaProperties { ...@@ -72,6 +72,12 @@ public class JpaProperties {
*/ */
private boolean showSql = false; 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(); private Hibernate hibernate = new Hibernate();
public Map<String, String> getProperties() { public Map<String, String> getProperties() {
...@@ -118,6 +124,14 @@ public class JpaProperties { ...@@ -118,6 +124,14 @@ public class JpaProperties {
this.showSql = showSql; this.showSql = showSql;
} }
public Boolean getOpenInView() {
return this.openInView;
}
public void setOpenInView(Boolean openInView) {
this.openInView = openInView;
}
public Hibernate getHibernate() { public Hibernate getHibernate() {
return this.hibernate; return this.hibernate;
} }
......
...@@ -145,12 +145,6 @@ ...@@ -145,12 +145,6 @@
"description": "Enable Mongo repositories.", "description": "Enable Mongo repositories.",
"defaultValue": true "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", "name": "spring.data.neo4j.repositories.enabled",
"type": "java.lang.Boolean", "type": "java.lang.Boolean",
...@@ -227,12 +221,6 @@ ...@@ -227,12 +221,6 @@
"description": "MBeanServer bean name.", "description": "MBeanServer bean name.",
"defaultValue": "mbeanServer" "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", "name": "spring.jta.enabled",
"type": "java.lang.Boolean", "type": "java.lang.Boolean",
......
...@@ -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 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`: don't want this behavior add the following to your `application.properties`:
[source,properties,indent=0]
---- ----
spring.data.neo4j.open-in-view=false spring.data.neo4j.open-in-view=false
---- ----
......
spring.h2.console.enabled=true spring.h2.console.enabled=true
spring.jpa.open-in-view=true
logging.level.org.hibernate.SQL=debug logging.level.org.hibernate.SQL=debug
endpoints.flyway.web.enabled=true endpoints.flyway.web.enabled=true
spring.jpa.hibernate.ddl-auto=validate spring.jpa.hibernate.ddl-auto=validate
spring.jpa.open-in-view=true
spring.h2.console.enabled=true spring.h2.console.enabled=true
\ No newline at end of file
logging.level.com.atomikos=WARN logging.level.com.atomikos=WARN
spring.artemis.embedded.queues=accounts spring.artemis.embedded.queues=accounts
\ No newline at end of file spring.jpa.open-in-view=true
spring.artemis.embedded.queues=accounts spring.artemis.embedded.queues=accounts
\ No newline at end of file spring.jpa.open-in-view=true
spring.jpa.generate-ddl=true
spring.datasource.jndi-name=java:jboss/datasources/bootdemo spring.datasource.jndi-name=java:jboss/datasources/bootdemo
spring.jpa.generate-ddl=true
spring.jpa.open-in-view=true
logging.level.com.arjuna=INFO
spring.artemis.embedded.queues=accounts spring.artemis.embedded.queues=accounts
logging.level.com.arjuna=INFO spring.jpa.open-in-view=true
\ No newline at end of file
spring.datasource.url=jdbc:mysql://localhost/doesnotexist spring.datasource.url=jdbc:mysql://localhost/doesnotexist
spring.jpa.open-in-view=true
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment