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-87c2c4800839master
parent
83e9944d34
commit
c6715c4fa9
10
pom.xml
10
pom.xml
|
@ -20,11 +20,6 @@
|
||||||
<artifactId>asm</artifactId>
|
<artifactId>asm</artifactId>
|
||||||
<version>3.3.1</version>
|
<version>3.3.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.aspectj</groupId>
|
|
||||||
<artifactId>aspectjrt</artifactId>
|
|
||||||
<version>1.5.4</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>br.com.rjconsultores</groupId>
|
<groupId>br.com.rjconsultores</groupId>
|
||||||
<artifactId>brazilutils</artifactId>
|
<artifactId>brazilutils</artifactId>
|
||||||
|
@ -505,6 +500,11 @@
|
||||||
<artifactId>WSAG</artifactId>
|
<artifactId>WSAG</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.aspectj</groupId>
|
||||||
|
<artifactId>aspectjweaver</artifactId>
|
||||||
|
<version>1.8.9</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jboss.spec.javax.servlet</groupId>
|
<groupId>org.jboss.spec.javax.servlet</groupId>
|
||||||
<artifactId>jboss-servlet-api_3.0_spec</artifactId>
|
<artifactId>jboss-servlet-api_3.0_spec</artifactId>
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,9 +9,9 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.apache.commons.collections.CollectionUtils;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.apache.commons.collections.CollectionUtils;
|
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
|
@ -32,16 +32,18 @@ import com.rjconsultores.ventaboletos.web.utilerias.spring.AppContext;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class ApplicationProperties {
|
public class ApplicationProperties {
|
||||||
|
|
||||||
private static Logger log = Logger.getLogger(ApplicationProperties.class);
|
private static Logger log = Logger.getLogger(ApplicationProperties.class);
|
||||||
private static ApplicationProperties INSTANCE;
|
private static ApplicationProperties INSTANCE;
|
||||||
private static Properties p;
|
private static Properties p;
|
||||||
private static final String PATH_IMAGENES = "/com/rjconsultores/ventaboletos/web/cliente/imagenes/";
|
private static final String PATH_IMAGENES = "/com/rjconsultores/ventaboletos/web/cliente/imagenes/";
|
||||||
|
|
||||||
|
private Boolean readOnlyConnection = Boolean.TRUE;
|
||||||
|
|
||||||
private ApplicationProperties() {
|
private ApplicationProperties() {
|
||||||
p = new Properties();
|
p = new Properties();
|
||||||
this.readConfiguration();
|
this.readConfiguration();
|
||||||
this.readConfigurationToDatabase();
|
this.readConfigurationToDatabase();
|
||||||
|
this.readReadOnlyConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ApplicationProperties getInstance() {
|
public static ApplicationProperties getInstance() {
|
||||||
|
@ -201,4 +203,18 @@ public class ApplicationProperties {
|
||||||
return 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue