Tomcat连接池方法
我正在开发一个 J2EE 应用程序,它将接收来自移动应用程序的请求并执行一些数据库操作以向用户显示数据。
所以数据库连接池对我来说非常重要。我在 J2EE 应用程序的 META-INF 文件夹中声明了 context.xml
。它看起来如下
<Context debug="0"
reloadable="true" crossContext="false" privileged="true" cookies="true" >
<Resource name="jdbc/servicedb"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
username="XXXXXX"
password="XXXXXX"
url="jdbc:oracle:thin:@XXXXXXXXXXX:XXXX:XXXXXXX"
initialSize="10"
maxActive="100"
maxIdle="50"
minIdle="10"
suspectTimeout="60"
timeBetweenEvictionRunsMillis="30000"
minEvictableIdleTimeMillis="60000"/>
</Context>
这是一个方法从我的连接类返回一个连接
public static Connection getConnection()
{
Connection dbConnection = null;
//loading from the dao.properties file
DAOProperties properties = new DAOProperties("com.jndi");
String url = properties.getProperty(PROPERTY_URL, true);
String driverClassName = properties.getProperty(PROPERTY_DRIVER, false);
String password = properties.getProperty(PROPERTY_PASSWORD, false);
String username = properties.getProperty(PROPERTY_USERNAME, password != null);
// If driver is specified, then load it to let it register itself with DriverManager.
if(driverClassName !=null){
try
{
Class.forName(driverClassName);
dbConnection = DriverManager.getConnection(url, username, password);
}catch(ClassNotFoundException e){
// Could not find the database driver
e.printStackTrace();
System.out.println("database driver error");
}catch(Exception e){
e.printStackTrace();
}
}
// Else assume URL as DataSource URL and lookup it in the JNDI.
else{
Context initContext;
DataSource ds;
try {
initContext = new InitialContext();
Context envContext = (Context)initContext.lookup(JNDI_ROOT);
if(envContext!=null){
ds = (DataSource)envContext.lookup(url);
if(ds!=null){
try {
dbConnection = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} catch (NamingException e) {
e.printStackTrace();
}
}
return dbConnection;
}
DAOProperties 是一个用于加载属性文件的包装类。我从 这里
我正在像这样关闭连接
public static void close(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
System.err.println("Closing Connection failed: " + e.getMessage());
e.printStackTrace();
}
}
}
最后我是使用连接来填充这样的列表
public loadlist(){
Connection dbConnection = null;
try{
dbConnection = Connection.getConnection();
if(dbConnection !=null){
System.out.println("Connected to database");
LOGGER.debug("Successfully connected to database");
else{
System.out.println("No Connection Exists");
LOGGER.debug("unable to connect to database");
}
}catch (SQLException e) {
// Could not connect to the database
e.printStackTrace();
System.out.println("database connection failed");
LOGGER.debug("database connection failed due to SQLException");
}finally{
Connection.close(dbConnection);
LOGGER.debug("connection closed");
}
return result;
}
刚才我已经经历了 这个关于连接池。
我不确定是否可以关闭连接或将其返回到池中?
我还想知道如何将连接返回到池中?
请帮忙。
I am developing a J2EE application which will receive requests from a mobile application and does some database operations to show the data to the user.
So database connection pooling is of great importance to me.I declared context.xml
in my META-INF folder of my J2EE application.It looks as follows
<Context debug="0"
reloadable="true" crossContext="false" privileged="true" cookies="true" >
<Resource name="jdbc/servicedb"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
username="XXXXXX"
password="XXXXXX"
url="jdbc:oracle:thin:@XXXXXXXXXXX:XXXX:XXXXXXX"
initialSize="10"
maxActive="100"
maxIdle="50"
minIdle="10"
suspectTimeout="60"
timeBetweenEvictionRunsMillis="30000"
minEvictableIdleTimeMillis="60000"/>
</Context>
And this is a method from my connection class which returns a connection
public static Connection getConnection()
{
Connection dbConnection = null;
//loading from the dao.properties file
DAOProperties properties = new DAOProperties("com.jndi");
String url = properties.getProperty(PROPERTY_URL, true);
String driverClassName = properties.getProperty(PROPERTY_DRIVER, false);
String password = properties.getProperty(PROPERTY_PASSWORD, false);
String username = properties.getProperty(PROPERTY_USERNAME, password != null);
// If driver is specified, then load it to let it register itself with DriverManager.
if(driverClassName !=null){
try
{
Class.forName(driverClassName);
dbConnection = DriverManager.getConnection(url, username, password);
}catch(ClassNotFoundException e){
// Could not find the database driver
e.printStackTrace();
System.out.println("database driver error");
}catch(Exception e){
e.printStackTrace();
}
}
// Else assume URL as DataSource URL and lookup it in the JNDI.
else{
Context initContext;
DataSource ds;
try {
initContext = new InitialContext();
Context envContext = (Context)initContext.lookup(JNDI_ROOT);
if(envContext!=null){
ds = (DataSource)envContext.lookup(url);
if(ds!=null){
try {
dbConnection = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} catch (NamingException e) {
e.printStackTrace();
}
}
return dbConnection;
}
DAOProperties is a wrapper class for loading the Properties file.I took it from here
I am closing the connection like this
public static void close(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
System.err.println("Closing Connection failed: " + e.getMessage());
e.printStackTrace();
}
}
}
And finally i am using the connection to polulate a list like this
public loadlist(){
Connection dbConnection = null;
try{
dbConnection = Connection.getConnection();
if(dbConnection !=null){
System.out.println("Connected to database");
LOGGER.debug("Successfully connected to database");
else{
System.out.println("No Connection Exists");
LOGGER.debug("unable to connect to database");
}
}catch (SQLException e) {
// Could not connect to the database
e.printStackTrace();
System.out.println("database connection failed");
LOGGER.debug("database connection failed due to SQLException");
}finally{
Connection.close(dbConnection);
LOGGER.debug("connection closed");
}
return result;
}
Just now i have gone through this on connection pooling.
I have an doubt whether i can close my connection or return it to the pool?
I also want to know how to return a connection to the pool?
Please help .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您的代码采用
getConnection()
方法中的 else 路径(即,它执行 JNDI 查找而不是使用 DriverManager),您的连接池的使用是正确的。您无法选择关闭连接还是将其返回到池中。如果您从池中获取了连接,
close()
会将其返回到池中(而不是关闭它)。没有其他选择。您只能关闭您自己创建的连接。Assuming that your code takes the else path in the
getConnection()
method (i.e. it does a JNDI lookup instead of using the DriverManager), your use of the connection pool is correct.You cannot choose between closing a connection and returning it to the pool. If you got the connection from the pool,
close()
will return it to the pool (instead of closing it). There's no other option. You can only close connection that you have created yourself.我不明白你为什么要编写自己的连接池。
我建议放弃这种方法并使用由 Tomcat 管理的 JNDI 数据源以及其他人编写的池。 Apache 有一个很好的 DBCP - 为什么要自己编写?
I don't see why you'd write your own connection pool.
I'd recommend scrapping this approach and going with JNDI data sources, managed by Tomcat, and a pool written by someone else. Apache has a good DBCP - why write your own?