[英]Getting Datasource instance which is configured in Application Server?
I have configured a Datasource in JBOSS EAP 6.3.2 using Admin console. 我已经使用管理控制台在JBOSS EAP 6.3.2中配置了数据源。
I want to know what are the ways in which we can get the instance of this Datasource? 我想知道以什么方式可以获取此数据源的实例? Can we use
@Resouce
, @Inject
annotations to get the Datasource? 我们可以使用
@Resouce
和@Inject
批注来获取数据源吗?
Or should we use the old JNDI lookup? 还是应该使用旧的JNDI查找?
Can anyone please help me understand this? 谁能帮我理解这一点?
I have never used Jboss EAP but as it's a Java EE 6 Server, you should be able to inject your datasource through @Resource
annotation. 我从未使用过Jboss EAP,但由于它是Java EE 6服务器,因此您应该能够通过
@Resource
注释注入数据源。
If it works the same as JBOSS AS 7, you should : 如果它与JBOSS AS 7相同,则应:
standalone.xml
standalone.xml
定义数据源 lookup
attribute of @Resource
@Resource
的lookup
属性的数据源 @Resource(lookup = "java:jboss/MyDataSource")
private DataSource m_dataSource;
I am using weblogic and I am using the lookup for getting connection. 我正在使用weblogic,并且正在使用查找来获得连接。 This may help you.
这可能对您有帮助。
public Connection getConnection(){
DataSource dataSource = null;
Connection connection = null;
try {
InitialContext initContext;
initContext = new InitialContext();
dataSource = (DataSource) initContext.lookup("jdbc/MyDatasource");
try {
connection = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}catch (Exception e) {
e.printStackTrace();
}
return connection;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.