Definir o banco (Principal/Replica) por método

bug#15223
dev:trevezani
qua:

git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Model/trunk/modelWeb@97920 d1611594-4594-4d17-8e1d-87c2c4800839
master
alberto 2019-09-27 19:45:12 +00:00
parent 83e9944d34
commit c6715c4fa9
6 changed files with 102 additions and 7 deletions

10
pom.xml
View File

@ -20,11 +20,6 @@
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>br.com.rjconsultores</groupId>
<artifactId>brazilutils</artifactId>
@ -505,6 +500,11 @@
<artifactId>WSAG</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.servlet</groupId>
<artifactId>jboss-servlet-api_3.0_spec</artifactId>

View File

@ -0,0 +1,18 @@
package com.rjconsultores.routing;
public class DynamicDataSourceHolder {
private static ThreadLocal<String> routeKey = new ThreadLocal<String>();
public static String getRouteKey() {
String key = routeKey.get();
return key;
}
public static void setRouteKey(String key) {
routeKey.set(key);
}
public static void removeRouteKey() {
routeKey.remove();
}
}

View File

@ -0,0 +1,10 @@
package com.rjconsultores.routing;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class MultipleDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DynamicDataSourceHolder.getRouteKey();
}
}

View File

@ -0,0 +1,12 @@
package com.rjconsultores.routing;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ReadOnlyConnection {
}

View File

@ -0,0 +1,39 @@
package com.rjconsultores.routing;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.rjconsultores.ventaboletos.utilerias.ApplicationProperties;
@Aspect
@Component
@Order(0)
public class ReadOnlyRouteInterceptor {
private static Logger log = Logger.getLogger(ReadOnlyRouteInterceptor.class);
@Around("@annotation(readOnlyConnection)")
public Object proceed(ProceedingJoinPoint proceedingJoinPoint, ReadOnlyConnection readOnlyConnection) throws Throwable {
if (ApplicationProperties.getInstance().getReadOnlyConnection()) {
try {
String className = proceedingJoinPoint.getSignature().getDeclaringTypeName();
String methodName = proceedingJoinPoint.getSignature().getName();
log.debug("*** READ *** [" + className + "." + methodName + "()");
} catch (Exception e) {}
try {
DynamicDataSourceHolder.setRouteKey("dataSourceRead");
return proceedingJoinPoint.proceed();
} finally {
DynamicDataSourceHolder.removeRouteKey();
}
} else {
return proceedingJoinPoint.proceed();
}
}
}

View File

@ -9,9 +9,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
@ -32,16 +32,18 @@ import com.rjconsultores.ventaboletos.web.utilerias.spring.AppContext;
*/
public class ApplicationProperties {
private static Logger log = Logger.getLogger(ApplicationProperties.class);
private static ApplicationProperties INSTANCE;
private static Properties p;
private static final String PATH_IMAGENES = "/com/rjconsultores/ventaboletos/web/cliente/imagenes/";
private Boolean readOnlyConnection = Boolean.TRUE;
private ApplicationProperties() {
p = new Properties();
this.readConfiguration();
this.readConfigurationToDatabase();
this.readReadOnlyConnection();
}
public static ApplicationProperties getInstance() {
@ -200,5 +202,19 @@ public class ApplicationProperties {
Application app = (Application) factory.getBean("app");
return app;
}
public void readReadOnlyConnection() {
ConstanteService constanteService = (ConstanteService) AppContext.getApplicationContext().getBean("constanteService");
Constante constante = constanteService.buscarPorNomeConstante("READ_ONLY_CONNECTION");
readOnlyConnection = Boolean.TRUE;
if ((constante != null) && StringUtils.isNotBlank(constante.getValorconstante())) {
readOnlyConnection = Boolean.valueOf(constante.getValorconstante());
}
}
public Boolean getReadOnlyConnection() {
return readOnlyConnection;
}
}