Java JEE将JNDI与JDBC和auth应用程序级别一起使用
为了简单起见,我们有Tomcat 8.0.27。在IDE下创建一个web项目 在web.xml中Java JEE将JNDI与JDBC和auth应用程序级别一起使用,java,jakarta-ee,jdbc,jndi,tomcat8,Java,Jakarta Ee,Jdbc,Jndi,Tomcat8,为了简单起见,我们有Tomcat 8.0.27。在IDE下创建一个web项目 在web.xml中 <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/tablemanager</res-ref-name> <res-type>javax.sql.DataSource</res-type>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/tablemanager</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
如果访问数据库的用户位于应用程序级别,则此功能非常有效
是否有严格的数据库用户管理,每个用户都有行级授权?也就是说,不使用容器身份验证,应用程序可以:
- 使用应用程序级授权。比如: 连接conn=ds.getConnection(“tablemanager”、“tablemanager”)
- 能否从池中检索连接(我怀疑这是不可能的,因为在Tomcat jdbc池中使用用户名和密码来测试查询)
- 如果池不合适,我可以定义一个资源并将其用于连接
<resource-ref>
<description>DB2 Connection</description>
<res-ref-name>jdbc/db2</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Application</res-auth>
<res-sharing-scope>Unshareable</res-sharing-scope>
</resource-ref>
<Resource
name="jdbc/db2"
auth="Application"
type="javax.sql.DataSource"
driverClassName="org.mariadb.jdbc.Driver"
url="jdbc:mysql://172.17.0.3:3306/tablemanager?allowMultiQueries=true"
/>
我明白了
BasicDataSource不支持
你用哪行代码抛出它?NB
BasicDataSource
是一个池DS,是Apache DBCP的一部分,我相信,您可以通过Tomcat免费获得它。@user207421 Connection conn=DS.getConnection(“tablemanager”,“tablemanager”);给我那个埃罗。我正在寻找是否有可能在应用程序srv中创建DS(数据源)并在使用时提供用户和密码。
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/tablemanager");
try (Connection conn = ds.getConnection()) {
<resource-ref>
<description>DB2 Connection</description>
<res-ref-name>jdbc/db2</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Application</res-auth>
<res-sharing-scope>Unshareable</res-sharing-scope>
</resource-ref>
<Resource
name="jdbc/db2"
auth="Application"
type="javax.sql.DataSource"
driverClassName="org.mariadb.jdbc.Driver"
url="jdbc:mysql://172.17.0.3:3306/tablemanager?allowMultiQueries=true"
/>
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/db2");
Connection conn = ds.getConnection("tablemanager", "tablemanager");