|
楼主 |
发表于 2020-1-25 23:54:01
|
显示全部楼层
基于又一天的思考,我说下我现在的方法:1,我是建的一个Listener来设置jndi的
public void contextInitialized(ServletContextEvent sce)
{
ServletContext application = sce.getServletContext();
Context env = null;
/**
* Get the JNDI and set pool to a atrribute
* Which we can use it in everywhere.
*/
try
{
env = (Context)new InitialContext().lookup("java:comp/env");
ds = (DataSource) env.lookup("jdbc/oracle/message_project");
if (ds == null)
{
application.log("Can not find jdbc/oracle/message_project JNDI");
}
}
catch (NamingException ne)
{
application.log("Can not get jdbc/oracle/message_project JNDI");
}
application.setAttribute("ds", ds);
}
2,然后是在tomcat5.0中配置数据库连接池的
<!-- 下面是我增加的 -->
<Resource name="jdbc/oracle/message_project"
auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/oracle/message_project">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<!-- Maximum number of idle dB connections to retain in pool.
Set to -1 for no limit. See also the DBCP documentation on this
and the minEvictableIdleTimeMillis configuration parameter.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<!-- MySQL dB username and password for dB connections -->
<parameter>
<name>username</name>
<value>System</value>
</parameter>
<parameter>
<name>password</name>
<value>sql</value>
</parameter>
<!-- Class name for the old mm.mysql JDBC driver - uncomment this entry and comment next
if you want to use this driver - we recommend using Connector/J though
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
-->
<!-- Class name for the official MySQL Connector/J driver -->
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:@localhost:1521:admin06</value>
</parameter>
</ResourceParams>
<!-- 上面是我增加的 -->
存在问题:1,我不知道这样做对与否 2,(我用的是eclipse+myeclipse)我在myeclipse里面始终找不到数据库的映射(如果用Use JDBC Driver来连接的话我可以找到自动的映射)为什么用jndi就找不到呢,当然我知道是我不知道。所以我希望各位不怜指教啊,在线等了一天一晚了。。 |
|