2012-07-10 57 views
0

我成功地在我的web應用程序(不使用Struts框架)中使用連接池。現在我需要集成由另一個開發人員編寫的部分(使用Struts 1框架的部分)。問題是我沒有成功地使用Struts 1池。Struts 1和連接池

應該有可能嗎?

在我的Tomcat的context.xml,我有:

<Resource name="jdbc/MyDB" auth="Container" type="javax.sql.DataSource" 
      maxActive="100" maxIdle="30" maxWait="10000" 
      username="auser" password="apwd" driverClassName="com.mysql.jdbc.Driver" 
      url="jdbc:mysql://localhost:3306/mydb" 
      factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" /> 

的其他開發人員使用的是servlet的連接上執行的,如:

Class.forName("com.mysql.jdbc.Driver"); 
con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "auser", "apwd"); 
+0

如您使用的是從池中獲取一個連接,另一部分應使用相同的代碼。開發人員的代碼不使用池,如果必須使用池,則不能保持原樣。您需要修復其他開發人員的代碼。 – 2012-07-10 10:29:17

回答

1

使用代碼這樣的事情在servlet的

// Obtain our environment naming context 
Context initCtx = new InitialContext(); 
Context envCtx = (Context) initCtx.lookup("java:comp/env"); 

// Look up our data source 
DataSource ds = (DataSource) 
    envCtx.lookup("jdbc/MyDB"); 

// Allocate and use a connection from the pool 
Connection conn = ds.getConnection(); 
... use this connection to access the database ... 
conn.close(); 

詳情請參考

http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html

http://viralpatel.net/blogs/database-connection-pooling-tomcat-eclipse-db/