简体   繁体   English

如何从数据源连接确定有关WebLogic数据源的信息?

[英]How to determine info about WebLogic datasource from datasource connection?

I have a Spring app running in WebLogic. 我有一个在WebLogic中运行的Spring应用程序。 In my DAO, it inherits a base class with a method that returns a "javax.sql.DataSource". 在我的DAO中,它继承了带有返回“ javax.sql.DataSource”的方法的基类。 I inject that datasource indirectly using the "jee:jndi-lookup" in my Spring application context to get the datasource from JNDI. 我在Spring应用程序上下文中使用“ jee:jndi-lookup”间接注入了该数据源,以从JNDI获取数据源。 When I look at this in the debugger, it appears to be a "weblogic.jdbc.common.internal.RmiDataSource". 当我在调试器中查看时,它似乎是“ weblogic.jdbc.common.internal.RmiDataSource”。

I'd like to figure out how I can introspect into that datasource in my code to determine information about the database I'm connected to, particularly the host, port, SID, and username. 我想弄清楚如何在代码中内省该数据源,以确定有关我连接的数据库的信息,尤其是主机,端口,SID和用户名。 Is there any way I could do that? 有什么办法可以做到吗?

For some background, I have extensive diagnostics in my app for troubleshooting db connection and query issues. 在某些背景下,我的应用程序中包含大量诊断程序,用于对数据库连接和查询问题进行故障排除。 It would be very helpful if at runtime, I could introspect information about the WebLogic datasource from the database connection in use. 如果在运行时我可以从正在使用的数据库连接中反思有关WebLogic数据源的信息,那将非常有帮助。

As I described, I already determined in the debugger what the actual type was, and I've examined all the obvious properties in that object for casting or reflection opportunities, and I don't see any obvious indications of information like "host", "port", or "SID". 正如我所描述的,我已经在调试器中确定了实际类型是什么,并且已经检查了该对象中所有明显的属性以进行强制转换或反射,并且看不到诸如“主机”之类的信息的明显迹象, “端口”或“ SID”。

I think you can use reflection and call all the getter methods of this class. 我认为您可以使用反射并调用此类的所有getter方法。
That should get you some helpful information. 那应该给您一些有用的信息。 But based on the name of this class, 但是根据此类的名称,
it looks like it's an internal WebLogic class and it's not really designed for this. 它看起来像是内部WebLogic类,并且并非为此真正设计的。
Still, you can do it with reflection, you can even call private getter methods with reflection. 不过,您可以使用反射来做到这一点,甚至可以使用反射来调用私有的getter方法。

Java Reflection API Java反射API

The "right" JEE approach is not caring about these low-level details in code. “正确的” JEE方法并不关心代码中的这些底层细节。 After finding out which DataSource doesn't work, troubleshooting problems involves only application server logs and application server configuration tweaks. 在找出哪个数据源不起作用之后,故障排除问题仅涉及应用程序服务器日志和应用程序服务器配置调整。

There are several good reasons for insulating applications from DataSource details: 有几个很好的理由使应用程序与DataSource详细信息隔离:

  • you aren't supposed to lose track of what abstract data sources you are using; 您不应丢失正在使用的抽象数据源的跟踪; using more than one DataSource (in fact, more than one Connection) in the same class is extremely unusual. 在同一个类中使用多个DataSource(实际上是多个Connection)非常不寻常。
  • permanent data source configurations have been defined by you in your application server configuration, they are not secret or hidden. 永久数据源配置已由您在应用程序服务器配置中定义,它们不是秘密的也不是隐藏的。
  • transient data source state, on the other hand, is accessible through application server tools and logs. 另一方面,可通过应用程序服务器工具和日志访问临时数据源状态。
  • on the third hand, data source implementations are proprietary and need to be freely optimized; 第三,数据源实现是专有的,需要自由优化。 expecting them to provide useful information is unreasonable. 期望他们提供有用的信息是不合理的。
  • your application is unlikely to be able to fix a troublesome data source; 您的应用程序不太可能能够修复麻烦的数据源; if it could, it would be able to mess with other applications in the same application server. 如果可能的话,它将能够与同一应用程序服务器中的其他应用程序发生混乱。
  • "replacing" a data source with another doesn't make any sense. 用另一个数据源“替换”没有任何意义。
  • any scenario in which you want the definition of data source to vary according to circumstances should be dealt with by other means, mostly close to the DBMS (for example, if DBMS processes or machines are subject to shutdowns and restarts you could configure the servers as a cluster sharing the same IP address; relatively easy with Oracle). 您希望数据源的定义根据情况而变化的任何情况都应通过其他方法来处理,通常应接近DBMS(例如,如果DBMS进程或计算机要关闭和重新启动,则可以将服务器配置为共享相同IP地址的集群;使用Oracle相对容易)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM