Skip to content

Latest commit

History

History
175 lines (131 loc) · 4.74 KB

File metadata and controls

175 lines (131 loc) · 4.74 KB

sqlcommenter-java

Introduction

Provides integrations to correlate user source code from various web frameworks with SQL comments from various ORMs.

When results are examined in SQL database logs, they'll look like this:

SELECT * from USERS /*action='run+this+%26+that',
controller='foo%3BDROP+TABLE+BAR',framework='spring,traceparent='00-9a4589fe88dd0fc911ff2233ffee7899-11fa8b00dd11eeff-01',tracestate='rojo%253D00f067aa0ba902b7%2Ccongo%253Dt61rcWkgMzE''*/

Integrations

ORMs

  • Hibernate
  • JDBC

Frameworks

  • Spring
  • Jetty
  • Netty
  • Apache Tomcat
  • gRPC

Using it

Spring

Java-based configuration

If your're using Spring 5, then you can add the SpringSQLCommenterInterceptor as follows:

importcom.google.cloud.sqlcommenter.interceptors.SpringSQLCommenterInterceptor;
@EnableWebMvc@ConfigurationpublicclassWebConfigimplementsWebMvcConfigurer {
@BeanpublicSpringSQLCommenterInterceptorsqlInterceptor() {
returnnewSpringSQLCommenterInterceptor();
}
@OverridepublicvoidaddInterceptors(InterceptorRegistryregistry) {
registry.addInterceptor(sqlInterceptor());
}
}

If you're using an older version of Spring, then your WebConfig class needs to extend the WebMvcConfigureAdapter class instead:

importcom.google.cloud.sqlcommenter.interceptors.SpringSQLCommenterInterceptor;
@EnableWebMvc@ConfigurationpublicclassWebConfigextendsWebMvcConfigureAdapter {
@BeanpublicSpringSQLCommenterInterceptorsqlInterceptor() {
returnnewSpringSQLCommenterInterceptor();
}
@OverridepublicvoidaddInterceptors(InterceptorRegistryregistry) {
registry.addInterceptor(sqlInterceptor());
}
}
XML-based configuration

You can also add the interceptor as a bean in your XML configuration:

<mvc:interceptors>
<beanclass="com.google.cloud.sqlcommenter.interceptors.SpringSQLCommenterInterceptor"></bean>
</mvc:interceptors>

or alternatively just for a specific method

<mvc:interceptors>
<mvc:interceptor>
<mvc:mappingpath="/flights"></mvc:mapping>
<beanclass="com.google.cloud.sqlcommenter.interceptors.SpringSQLCommenterInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>

Hibernate

Hibernate via JPA

If you're using Hibernate via JPA, then you can simply set the hibernate.session_factory.statement_inspector configuration property in the persistence.xml configuration file:

<propertyname="hibernate.session_factory.statement_inspector"value="com.google.cloud.sqlcommenter.schibernate.SCHibernate" />
Hibernate via Spring Application Properties

If you're using Hibernate via Spring application properties(i.e. you have application.yml or application.properties in your resources folder)

  • application.properties
spring.jpa.properties.hibernate.session_factory.statement_inspector=com.google.cloud.sqlcommenter.schibernate.SCHibernate
  • application.yml
spring:
jpa:
properties:
hibernate:
session_factory:
statement_inspector: com.google.cloud.sqlcommenter.schibernate.SCHibernate
Hibernate via Spring

If you're using Hibernate via Spring, then you might not use a persistence.xml configuration file, in which case, you can set up the hibernate.session_factory.statement_inspector configuration property as follows:

@Configuration@EnableTransactionManagementpublicclassJPAConfig {
@BeanpublicLocalContainerEntityManagerFactoryBeanentityManagerFactory() {
LocalContainerEntityManagerFactoryBeanem = newLocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(newString[] { "you.application.domain.model" });
em.setJpaVendorAdapter(newHibernateJpaVendorAdapter());
em.setJpaProperties(additionalProperties());
returnem;
}
privatePropertiesadditionalProperties() {
Propertiesproperties = newProperties();
properties.setProperty("hibernate.session_factory.statement_inspector", SCHibernate.class.getName());
returnproperties;
}
}

Spring Hibernate

  1. Please follow the instructions to add the Spring interceptor
  2. Please follow the instructions to add the Hibernate StatementInspector