Original code: / * * Created on 2004-8-11 * * Todo to change the template for this generated file go to * window - preferences - java - code style - code templates * / package saoc
Import java.sql.connection; import java.sql.driverManager; import java.sql.sqlexception;
Import javax.servlet.servletException; import javax.servlet.unavailableException;
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.action.ActionServlet; import org.apache.struts.action.PlugIn; import org.apache.struts .config.Moduleconfig;
/ ** * @author Huang Jinxiang * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates * / public class MyConnectionPlugIn implements PlugIn {
/ * (Non-Javadoc) * @see org.apache.struts.action.PlugIn # destroy () * / private static Log log = LogFactory.getLog (MyConnectionPlugIn.class); private static Connection connection; private String url = null; private String username = null; private String password = null; private String className = null; public String getClassName () {return className;} public void setClassName (String className) {this.className = className;} public String getPassword () {return password;} public void setPassword (String password) {this.password = password;} public String getUrl () {return url;} public void setUrl (String url) {this.url = url;} public String getUsername () {return username;} public void setUsername (String username) {this.username = username;} public void destroy () {// TODO Auto-generated method stub try {connection.close ();} catch (SQLException se) {}}
/ * (non-javadoc) * @see org.apache.struts.action.plugin # init (org.apache.struts.Action.ActionServlet, org.apache.struts.config.moduleconfig) * / public void init (ActionServlet Arg0 , ModuleConfig arg1) throws ServletException {// TODO Auto-generated method stub try {Class.forName (className); connection = DriverManager.getConnection (url, username, password);} catch (SQLException se) {log.error (se. getMessage (), se); throw new UnavailableException ( "No Resource!");} catch (ClassNotFoundException e) {// TODO Auto-generated catch block log.error (e.getMessage (), e); throw new UnavailableException ( "No Resource!");}} / ** * @return returns the conn () {return connection;}} is only required to join PLUG-I in Struts-Config.xml:
You can then take the current Connection via MyConnectionPlugin.getConnection (). .
This ensures that the entire Application is only a unique connection. .
Of course, you can also be configured by DataSource. .