You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "RamKung22 (via GitHub)" <gi...@apache.org> on 2023/09/14 04:03:16 UTC

[GitHub] [shardingsphere] RamKung22 commented on issue #22356: SpringCloud+Seata1.5.2+ShardingSphereJDBC5.2.1 SeataATShardingSphereTransactionManager.commit throw null exception

RamKung22 commented on issue #22356:
URL: https://github.com/apache/shardingsphere/issues/22356#issuecomment-1718731279

   > Rewrite the SeataTransactionHolder class to make it public, add an interceptor and manually call SeataTransactionHolder.set(), it can work normally, but I don't know if it is correct.
   > 
   > ```java
   > public final class SeataTransactionHolder {
   > 
   >     private SeataTransactionHolder() {
   > 
   >     }
   > 
   >     private static final ThreadLocal<GlobalTransaction> CONTEXT = new ThreadLocal<>();
   > 
   >     /**
   >      * Set seata global transaction.
   >      *
   >      * @param transaction global transaction context
   >      */
   >     public static void set(final GlobalTransaction transaction) {
   >         CONTEXT.set(transaction);
   >     }
   > 
   >     /**
   >      * Get seata global transaction.
   >      *
   >      * @return global transaction
   >      */
   >     public static GlobalTransaction get() {
   >         return CONTEXT.get();
   >     }
   > 
   >     /**
   >      * Clear global transaction.
   >      */
   >     public static void clear() {
   >         CONTEXT.remove();
   >     }
   > }
   > ```
   > 
   > ```java
   > @Configuration
   > public class SeataFeignInterceptorConfiguration implements WebMvcConfigurer {
   > 
   >     @Override
   >     public void addInterceptors(InterceptorRegistry registry) {
   >         registry.addInterceptor(new SeataFeignInterceptor()).addPathPatterns("/**");
   >     }
   > 
   >     public static class SeataFeignInterceptor implements HandlerInterceptor {
   > 
   >         @Override
   >         public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
   >             String xid = RootContext.getXID();
   >             if (xid == null) {
   >                 xid = request.getHeader(RootContext.KEY_XID);
   >             }
   > 
   >             if (!StringUtils.isBlank(xid) && SeataTransactionHolder.get() == null) {
   >                 RootContext.bind(xid);
   >                 SeataTransactionHolder.set(GlobalTransactionContext.getCurrentOrCreate());
   >             }
   > 
   >             return true;
   >         }
   >     }
   > }
   > ```
   
   We also solved the problem in the same way, but we don't know if there will be other effects


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org