Skip to content

Commit

Permalink
Log warning when open-in-view is implicitly enabled for JPA or Neo4j
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Oct 30, 2017
1 parent 95d8fe7 commit 5aa6630
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

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;

Expand Down Expand Up @@ -117,8 +119,23 @@ protected static class Neo4jWebConfiguration {
@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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -102,6 +108,14 @@ public void setAutoIndex(AutoIndexMode autoIndex) {
this.autoIndex = autoIndex;
}

public Boolean getOpenInView() {
return this.openInView;
}

public void setOpenInView(Boolean openInView) {
this.openInView = openInView;
}

public Embedded getEmbedded() {
return this.embedded;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
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;
Expand Down Expand Up @@ -214,8 +217,23 @@ protected static class JpaWebConfiguration {
@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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -118,6 +124,14 @@ public void setShowSql(boolean showSql) {
this.showSql = showSql;
}

public Boolean getOpenInView() {
return this.openInView;
}

public void setOpenInView(Boolean openInView) {
this.openInView = openInView;
}

public Hibernate getHibernate() {
return this.hibernate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
----
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
spring.h2.console.enabled=true

spring.jpa.open-in-view=true
logging.level.org.hibernate.SQL=debug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# spring.data.neo4j.open-in-view=true
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.jpa.open-in-view=true
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
spring.artemis.embedded.queues=accounts
spring.artemis.embedded.queues=accounts
spring.jpa.open-in-view=true
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
spring.datasource.url=jdbc:mysql://localhost/doesnotexist
spring.jpa.open-in-view=true

0 comments on commit 5aa6630

Please sign in to comment.