{"id":12136,"date":"2024-03-14T15:15:37","date_gmt":"2024-03-14T15:15:37","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/"},"modified":"2025-08-04T21:51:38","modified_gmt":"2025-08-04T21:51:38","slug":"how-does-mybatis-interceptor-determine-the-data-source","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/","title":{"rendered":"MyBatis Interceptor: Data Source Detection"},"content":{"rendered":"<p>MyBatis Interceptor is a plugin used to intercept the process of SQL execution, allowing for operations such as printing the SQL statement and recording execution time before and after executing the SQL statement.<\/p>\n<p>To determine the data source, you can access the current SqlSession object in the Interceptor&#8217;s interception method, then retrieve the data source information from the SqlSession object.<\/p>\n<p>Here is a simple example code:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">DataSourceInterceptor<\/span> <span class=\"hljs-keyword\">implements<\/span> <span class=\"hljs-title class_\">Interceptor<\/span> {\r\n\r\n    <span class=\"hljs-meta\">@Override<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> Object <span class=\"hljs-title function_\">intercept<\/span><span class=\"hljs-params\">(Invocation invocation)<\/span> <span class=\"hljs-keyword\">throws<\/span> Throwable {\r\n        <span class=\"hljs-comment\">\/\/ \u83b7\u53d6\u5f53\u524d\u6267\u884c\u7684SqlSession\u5bf9\u8c61<\/span>\r\n        <span class=\"hljs-type\">SqlSession<\/span> <span class=\"hljs-variable\">sqlSession<\/span> <span class=\"hljs-operator\">=<\/span> (SqlSession) invocation.getArgs()[<span class=\"hljs-number\">0<\/span>];\r\n        \r\n        <span class=\"hljs-comment\">\/\/ \u83b7\u53d6\u5f53\u524d\u6570\u636e\u6e90\u4fe1\u606f<\/span>\r\n        <span class=\"hljs-type\">DataSource<\/span> <span class=\"hljs-variable\">dataSource<\/span> <span class=\"hljs-operator\">=<\/span> sqlSession.getConfiguration().getEnvironment().getDataSource();\r\n        \r\n        <span class=\"hljs-comment\">\/\/ \u5224\u65ad\u6570\u636e\u6e90\u7c7b\u578b<\/span>\r\n        <span class=\"hljs-keyword\">if<\/span>(dataSource <span class=\"hljs-keyword\">instanceof<\/span> PooledDataSource) {\r\n            System.out.println(<span class=\"hljs-string\">\"\u4f7f\u7528\u7684\u6570\u636e\u6e90\u662fPooledDataSource\"<\/span>);\r\n        } <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span>(dataSource <span class=\"hljs-keyword\">instanceof<\/span> UnpooledDataSource) {\r\n            System.out.println(<span class=\"hljs-string\">\"\u4f7f\u7528\u7684\u6570\u636e\u6e90\u662fUnpooledDataSource\"<\/span>);\r\n        } <span class=\"hljs-keyword\">else<\/span> {\r\n            System.out.println(<span class=\"hljs-string\">\"\u4f7f\u7528\u7684\u6570\u636e\u6e90\u672a\u77e5\u7c7b\u578b\"<\/span>);\r\n        }\r\n        \r\n        <span class=\"hljs-comment\">\/\/ \u6267\u884c\u539f\u59cb\u65b9\u6cd5<\/span>\r\n        <span class=\"hljs-keyword\">return<\/span> invocation.proceed();\r\n    }\r\n\r\n    <span class=\"hljs-meta\">@Override<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> Object <span class=\"hljs-title function_\">plugin<\/span><span class=\"hljs-params\">(Object target)<\/span> {\r\n        <span class=\"hljs-keyword\">return<\/span> Plugin.wrap(target, <span class=\"hljs-built_in\">this<\/span>);\r\n    }\r\n\r\n    <span class=\"hljs-meta\">@Override<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title function_\">setProperties<\/span><span class=\"hljs-params\">(Properties properties)<\/span> {\r\n        <span class=\"hljs-comment\">\/\/ Do nothing<\/span>\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>In the example above, we have implemented a DataSourceInterceptor that intercepts the method &#8216;intercept&#8217; to retrieve the current SqlSession object. We use the SqlSession to obtain information about the data source in order to determine its type. Based on the type of data source, we can perform different actions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MyBatis Interceptor is a plugin used to intercept the process of SQL execution, allowing for operations such as printing the SQL statement and recording execution time before and after executing the SQL statement. To determine the data source, you can access the current SqlSession object in the Interceptor&#8217;s interception method, then retrieve the data source [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[4093,7774,1978,1459,11394],"class_list":["post-12136","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-data-source","tag-interceptor","tag-java-framework","tag-mybatis","tag-sql-session"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>MyBatis Interceptor: Data Source Detection - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn how MyBatis interceptor determines the data source by accessing the SqlSession object. Get code examples and implementation guide.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MyBatis Interceptor: Data Source Detection\" \/>\n<meta property=\"og:description\" content=\"Learn how MyBatis interceptor determines the data source by accessing the SqlSession object. Get code examples and implementation guide.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-14T15:15:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-04T21:51:38+00:00\" \/>\n<meta name=\"author\" content=\"Olivia Parker\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Olivia Parker\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/\"},\"author\":{\"name\":\"Olivia Parker\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9\"},\"headline\":\"MyBatis Interceptor: Data Source Detection\",\"datePublished\":\"2024-03-14T15:15:37+00:00\",\"dateModified\":\"2025-08-04T21:51:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/\"},\"wordCount\":121,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"Data Source\",\"interceptor\",\"Java framework\",\"MyBatis\",\"SQL Session\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/\",\"name\":\"MyBatis Interceptor: Data Source Detection - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-14T15:15:37+00:00\",\"dateModified\":\"2025-08-04T21:51:38+00:00\",\"description\":\"Learn how MyBatis interceptor determines the data source by accessing the SqlSession object. Get code examples and implementation guide.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MyBatis Interceptor: Data Source Detection\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9\",\"name\":\"Olivia Parker\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g\",\"caption\":\"Olivia Parker\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/oliviaparker\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"MyBatis Interceptor: Data Source Detection - Blog - Silicon Cloud","description":"Learn how MyBatis interceptor determines the data source by accessing the SqlSession object. Get code examples and implementation guide.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/","og_locale":"en_US","og_type":"article","og_title":"MyBatis Interceptor: Data Source Detection","og_description":"Learn how MyBatis interceptor determines the data source by accessing the SqlSession object. Get code examples and implementation guide.","og_url":"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-14T15:15:37+00:00","article_modified_time":"2025-08-04T21:51:38+00:00","author":"Olivia Parker","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Olivia Parker","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/"},"author":{"name":"Olivia Parker","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9"},"headline":"MyBatis Interceptor: Data Source Detection","datePublished":"2024-03-14T15:15:37+00:00","dateModified":"2025-08-04T21:51:38+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/"},"wordCount":121,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["Data Source","interceptor","Java framework","MyBatis","SQL Session"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/","url":"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/","name":"MyBatis Interceptor: Data Source Detection - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-14T15:15:37+00:00","dateModified":"2025-08-04T21:51:38+00:00","description":"Learn how MyBatis interceptor determines the data source by accessing the SqlSession object. Get code examples and implementation guide.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-does-mybatis-interceptor-determine-the-data-source\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"MyBatis Interceptor: Data Source Detection"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9","name":"Olivia Parker","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g","caption":"Olivia Parker"},"url":"https:\/\/www.silicloud.com\/blog\/author\/oliviaparker\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/12136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=12136"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/12136\/revisions"}],"predecessor-version":[{"id":155938,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/12136\/revisions\/155938"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=12136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=12136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=12136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}