forked from apache/incubator-seata
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix:fix wrong proxy of datasource bean (apache#2323)
- Loading branch information
1 parent
8c4ca8f
commit 1d468c4
Showing
10 changed files
with
167 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
package io.seata.spring.boot.autoconfigure; | ||
|
||
import io.seata.spring.annotation.GlobalTransactionScanner; | ||
import io.seata.spring.annotation.datasource.SeataDataSourceBeanPostProcessor; | ||
import io.seata.spring.annotation.datasource.SeataAutoDataSourceProxyCreator; | ||
import io.seata.spring.boot.autoconfigure.properties.SeataProperties; | ||
import io.seata.spring.boot.autoconfigure.provider.SpringApplicationContextProvider; | ||
import org.slf4j.Logger; | ||
|
@@ -30,7 +30,7 @@ | |
import org.springframework.context.annotation.DependsOn; | ||
|
||
import static io.seata.common.Constants.BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER; | ||
import static io.seata.spring.annotation.datasource.AutoDataSourceProxyRegistrar.BEAN_NAME_SEATA_DATA_SOURCE_BEAN_POST_PROCESSOR; | ||
import static io.seata.spring.annotation.datasource.AutoDataSourceProxyRegistrar.BEAN_NAME_SEATA_AUTO_DATA_SOURCE_PROXY_CREATOR; | ||
|
||
/** | ||
* @author [email protected] | ||
|
@@ -58,10 +58,10 @@ public GlobalTransactionScanner globalTransactionScanner(SeataProperties seataPr | |
return new GlobalTransactionScanner(seataProperties.getApplicationId(), seataProperties.getTxServiceGroup()); | ||
} | ||
|
||
@Bean(BEAN_NAME_SEATA_DATA_SOURCE_BEAN_POST_PROCESSOR) | ||
@Bean(BEAN_NAME_SEATA_AUTO_DATA_SOURCE_PROXY_CREATOR) | ||
@ConditionalOnProperty(prefix = StarterConstants.SEATA_PREFIX, name = {"enableAutoDataSourceProxy", "enable-auto-data-source-proxy"}, havingValue = "true", matchIfMissing = true) | ||
@ConditionalOnMissingBean(SeataDataSourceBeanPostProcessor.class) | ||
public SeataDataSourceBeanPostProcessor seataDataSourceBeanPostProcessor(SeataProperties seataProperties) { | ||
return new SeataDataSourceBeanPostProcessor(seataProperties.isUseJdkProxy()); | ||
@ConditionalOnMissingBean(SeataAutoDataSourceProxyCreator.class) | ||
public SeataAutoDataSourceProxyCreator seataAutoDataSourceProxyCreator(SeataProperties seataProperties) { | ||
return new SeataAutoDataSourceProxyCreator(seataProperties.isUseJdkProxy(),seataProperties.getExcludesForAutoProxying()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...g/src/main/java/io/seata/spring/annotation/datasource/SeataAutoDataSourceProxyAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.seata.spring.annotation.datasource; | ||
|
||
import javax.sql.DataSource; | ||
import java.lang.reflect.Method; | ||
|
||
import io.seata.rm.datasource.DataSourceProxy; | ||
import org.aopalliance.intercept.MethodInterceptor; | ||
import org.aopalliance.intercept.MethodInvocation; | ||
import org.springframework.aop.IntroductionInfo; | ||
import org.springframework.beans.BeanUtils; | ||
|
||
/** | ||
* @author [email protected] | ||
*/ | ||
public class SeataAutoDataSourceProxyAdvice implements MethodInterceptor, IntroductionInfo { | ||
|
||
@Override | ||
public Object invoke(MethodInvocation invocation) throws Throwable { | ||
DataSourceProxy dataSourceProxy = DataSourceProxyHolder.get().putDataSource((DataSource) invocation.getThis()); | ||
Method method = invocation.getMethod(); | ||
Object[] args = invocation.getArguments(); | ||
Method m = BeanUtils.findDeclaredMethod(DataSourceProxy.class, method.getName(), method.getParameterTypes()); | ||
if (null != m) { | ||
return m.invoke(dataSourceProxy, args); | ||
} else { | ||
return invocation.proceed(); | ||
} | ||
} | ||
|
||
@Override | ||
public Class<?>[] getInterfaces() { | ||
return new Class[]{SeataProxy.class}; | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
.../src/main/java/io/seata/spring/annotation/datasource/SeataAutoDataSourceProxyCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.seata.spring.annotation.datasource; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.Arrays; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.aop.Advisor; | ||
import org.springframework.aop.TargetSource; | ||
import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator; | ||
import org.springframework.aop.support.DefaultIntroductionAdvisor; | ||
import org.springframework.beans.BeansException; | ||
|
||
/** | ||
* @author [email protected] | ||
*/ | ||
public class SeataAutoDataSourceProxyCreator extends AbstractAutoProxyCreator { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(SeataAutoDataSourceProxyCreator.class); | ||
private final String[] excludes; | ||
private final Advisor advisor = new DefaultIntroductionAdvisor(new SeataAutoDataSourceProxyAdvice()); | ||
|
||
public SeataAutoDataSourceProxyCreator(boolean useJdkProxy, String[] excludes) { | ||
this.excludes = excludes; | ||
setProxyTargetClass(!useJdkProxy); | ||
} | ||
|
||
@Override | ||
protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, TargetSource customTargetSource) throws BeansException { | ||
if (LOGGER.isInfoEnabled()) { | ||
LOGGER.info("Auto proxy of [{}]", beanName); | ||
} | ||
return new Object[]{advisor}; | ||
} | ||
|
||
@Override | ||
protected boolean shouldSkip(Class<?> beanClass, String beanName) { | ||
return SeataProxy.class.isAssignableFrom(beanClass) || | ||
!DataSource.class.isAssignableFrom(beanClass) || | ||
Arrays.asList(excludes).contains(beanClass.getName()); | ||
} | ||
} |
108 changes: 0 additions & 108 deletions
108
...src/main/java/io/seata/spring/annotation/datasource/SeataDataSourceBeanPostProcessor.java
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
spring/src/main/java/io/seata/spring/annotation/datasource/SeataProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.seata.spring.annotation.datasource; | ||
|
||
/** | ||
* @author [email protected] | ||
*/ | ||
public interface SeataProxy { | ||
} |