-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the issue of database write prohibition failure in some versions …
…of Postgresql Signed-off-by: hanbingleixue <hanbingleixue@hotmail.com>
- Loading branch information
1 parent
de55650
commit 90b58da
Showing
5 changed files
with
208 additions
and
6 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...in/src/main/java/com/huaweicloud/sermant/postgresqlv42/declarers/PgStatementDeclarer.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,41 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* 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 com.huaweicloud.sermant.postgresqlv42.declarers; | ||
|
||
import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; | ||
import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; | ||
import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; | ||
import com.huaweicloud.sermant.postgresqlv42.utils.PostgresqlEnhancementHelper; | ||
|
||
/** | ||
* PgStatement declarer | ||
* | ||
* @author zhp | ||
* @since 2024-03-19 | ||
**/ | ||
public class PgStatementDeclarer extends AbstractPluginDeclarer { | ||
@Override | ||
public ClassMatcher getClassMatcher() { | ||
return PostgresqlEnhancementHelper.getPgStatementClassMatcher(); | ||
} | ||
|
||
@Override | ||
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { | ||
return new InterceptDeclarer[]{PostgresqlEnhancementHelper.getPgStatementExecuteInterceptDeclarer(), | ||
PostgresqlEnhancementHelper.getPgStatementExecuteBatchInterceptDeclarer()}; | ||
} | ||
} |
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
87 changes: 87 additions & 0 deletions
87
.../main/java/com/huaweicloud/sermant/postgresqlv42/interceptors/PgStatementInterceptor.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,87 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* 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 com.huaweicloud.sermant.postgresqlv42.interceptors; | ||
|
||
import com.huaweicloud.sermant.core.common.LoggerFactory; | ||
import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; | ||
import com.huaweicloud.sermant.database.handler.DatabaseHandler; | ||
import com.huaweicloud.sermant.database.interceptor.AbstractDatabaseInterceptor; | ||
import com.huaweicloud.sermant.database.utils.ThreadDatabaseUrlUtil; | ||
|
||
import org.postgresql.jdbc.PgStatement; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* Interceptor for PgStatement execute and executeBatch method | ||
* | ||
* @author zhp | ||
* @since 2024-03-19 | ||
**/ | ||
public class PgStatementInterceptor extends AbstractDatabaseInterceptor { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(); | ||
|
||
/** | ||
* Non-parametric construction method | ||
*/ | ||
public PgStatementInterceptor() { | ||
} | ||
|
||
/** | ||
* Parameterized construction method | ||
* | ||
* @param handler Database write operation handler | ||
*/ | ||
public PgStatementInterceptor(DatabaseHandler handler) { | ||
this.handler = handler; | ||
} | ||
|
||
@Override | ||
public ExecuteContext doBefore(ExecuteContext context) { | ||
PgStatement statement = (PgStatement) context.getObject(); | ||
|
||
// Store link information to ensure that QueryExecutorImplInterceptor can retrieve database information | ||
try { | ||
Connection connection = statement.getConnection(); | ||
if (connection != null && connection.getMetaData() != null) { | ||
ThreadDatabaseUrlUtil.setDatabaseUrl(connection.getMetaData().getURL()); | ||
} | ||
} catch (SQLException e) { | ||
LOGGER.log(Level.SEVERE, "can not obtain the database information.", e); | ||
} | ||
return context; | ||
} | ||
|
||
@Override | ||
protected void createAndCacheDatabaseInfo(ExecuteContext context) { | ||
// This interception point is mainly used to store link information into thread variables, without the need to | ||
// cache basic database information, so the method is implemented as empty | ||
} | ||
|
||
@Override | ||
public ExecuteContext after(ExecuteContext context) throws Exception { | ||
if (handler != null) { | ||
handler.doAfter(context); | ||
return context; | ||
} | ||
ThreadDatabaseUrlUtil.removeDatabaseUrl(); | ||
return context; | ||
} | ||
} |
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