TOMCAT+MYSQL+JAVA+JSP开发一个小网页游戏网站。
需要控制数据库连接数目,控制内存使用量,优化WEB每秒请求数。
如下为要求,需要用到哪些技术?
  JVM最大内存 80 MB 
  JSP空间磁盘总量 500 MB 
  WEB每秒请求数 50 
  数据库最大连接数 15 

解决方案 »

  1.   

    1. JVM最大内存 80 MB 
    采用共享的单件模式共享重用的数据的内容。如只读的数据库,假设共占10M。
    每个客户端1个SESSION,一些临时变量,假设为10K,那么理论上支持700个客户端同时浏览。
    2. JSP空间磁盘总量 500 MB 
    这个问题应该不大
    3.  WEB每秒请求数 50 
    不知如何控制和优化。如果客户端同时使用WEB请求率为10%,则理论上支持500个客户端同时浏览。
    4.  数据库最大连接数 15 
    单件数据连接池,使用数据库库是从数据库连接池中取连接,用完归还(不关闭)。这样理论上支持15个连接同时进行,如果客户端同时使用连接率为5%,怎理论上支持300个客户端同时浏览。
      

  2.   

    部分代码。
    index.jsp
    <jsp:useBean id="mdb" class="mysql.Mysqlconn" scope="application"/>
    <jsp:useBean id="dataBuffer" class="mysql.DataBuffer" scope="application" />
    <jsp:useBean id="dbAccess" class="mysql.DbAccess" scope="session" />
    <jsp:useBean id="toolKit" class="mysql.ToolKit" scope="page" />Mysqlconn.javapackage mysql;
    import java.sql.*;
    import java.util.*;
    import javax.sql.DataSource;
    import javax.naming.*;//this class will be used as application mode, all clients have single instance.
    public class Mysqlconn 
    {
    static private Mysqlconn instance = new Mysqlconn();
    static private String sConnStr; //Unused
    // static private Connection conn = null; //current connection
    // static private Statement statement = null;//current
    // static private ResultSet rs = null;//current
        //正在使用连接的数量
    static private int inUseCount = 0; // equals to usingConnections.size()
        //目前可用的连接数,即空闲连接
    static private Vector<Connection> connections = new Vector<Connection>();
    //目前已取出使用中的连接数
    static private Vector<Connection> usingConnections = new Vector<Connection>();
        //最大连接数
        static private int maxconn = 15;
        
        static public Mysqlconn GetInstance()
        {
         return instance;
        }/*将空闲连接返回给连接池*/
    private synchronized static void RecycleConnection(Connection conn)
        {
         if (conn != null)
         {
            //将指定连接加到向量末尾
            connections.addElement(conn);
            usingConnections.removeElement(conn);
            //连接用户减一
            inUseCount--;
         }
        }    /*从连接池得到一个连接*/
        private synchronized static Connection GetConnection()
        {
         Connection conn = null; //Connection是一个类,
         try
         {
            //connections是一个向量,用于存储连接对象,它所存储是的所有空闲状态的可用连接
            if (connections.size() > 0) 
            {
                //获取连接列表的第一个连接
                conn = (Connection) connections.elementAt(0);
                connections.removeElementAt(0);//获得一个连接,并将此连接从队列中删除.

                //如果此连接已关闭,刚继续获取,
                try {
                    if (conn.isClosed())
                        conn = GetConnection();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
            //如果实际使用的连接数小于最大连接数即有可用连接),就新增加一个连接
            else if (maxconn == 0 || inUseCount < maxconn)
            {
                //如此时无可用连接(maxconn == 0)且连接数又未达到上限(inUseCount < maxconn)),就创建一个新连接
                conn = CreateConnection();
            }
            else
            {
             ResetUsingConnections();
             conn = GetConnection();
            }
            //如果连接数已达到上限就返回空指针
            if (conn != null)
            {
            usingConnections.addElement(conn);
                inUseCount++;
            }
         }catch(Exception e){
                e.printStackTrace();
                return null;
            }
            return conn;
        }    /*创建新的连接*/
        private synchronized static Connection CreateConnection()
        {
            Connection conn=null;
            try
            {
                conn = GetNewConnection();
            }catch(Exception e){
                e.printStackTrace();
                return null;
            }
            return conn;
        }    /*关闭所有空闲的连接*/
        private synchronized static void CloseFreeConnections()
        {
            Enumeration<Connection> allConnections = connections.elements();
            while (allConnections.hasMoreElements())
            {
                Connection conn=(Connection) allConnections.nextElement();
                try{
                    conn.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }
            }
            connections.removeAllElements();
        }
        
        /*关闭所有正在使用的连接*/
        private synchronized static void CloseUsingConnections()
        {
            Enumeration<Connection> allConnections = usingConnections.elements();
            while (allConnections.hasMoreElements())
            {
                Connection conn=(Connection) allConnections.nextElement();
                try{
                    conn.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }
            }
            usingConnections.removeAllElements();
            inUseCount = 0; 
        }
        
        private synchronized static void ResetUsingConnections()
        {
         CloseFreeConnections();
            connections.addAll(usingConnections);
            usingConnections.removeAllElements();
            inUseCount = 0; 
        }

        private synchronized static Connection GetNewConnection()
    {
    DataSource ds = null;
    DataSource ds2 = null;
    try
    {
    InitialContext ctx = new InitialContext();
    ds = (DataSource)ctx.lookup("java:comp/env/jdbc/rollerdb"); //hard code
    ds2 = (DataSource)ctx.lookup("java:comp/env/jdbc/rollerdb2"); //hard code
    }
        catch (Exception ex) 
        {
         System.err.println("Error:GetConnection:"+ ex.getMessage());
            return null;
        }
        
    Connection conn = null;
    try 
    {
        conn = ds.getConnection();
        if (conn == null)
         conn = ds2.getConnection();

        catch (Exception ex) 
        {
         System.err.println("Error:GetConnection:"+ ex.getMessage());
         if (conn != null) {
                try {
                    conn.close();
                } catch (Exception sqlex) {
                }
                conn = null;
        }
            return null;
        }
    return conn;
    }
    ...
    }
        
    package mysql;
    import java.sql.ResultSet;
    import java.sql.SQLException;import java.util.*;
    import mysql.DbAccess.*;
    public class DataBuffer
    {
    static private DataBuffer instance = new DataBuffer();
    //private Mysqlconn mdb = Mysqlconn.GetInstance();

    static public ArrayList<CBuildInfo> buildInfoBuffer = new ArrayList<CBuildInfo>();
    static public ArrayList<CTroopInfo> troopInfoBuffer = new ArrayList<CTroopInfo>();

    static public DataBuffer GetInstance()
    {
    return instance;
    }

    public boolean ExtractAll() //extract all data from database
    {
    ExtractBuildInfo();
    ExtractTroopInfo();
    return true;
    }
    ...
    }
      

  3.   

    把数据交给  hibernate来处理 或许要简单多了
      

  4.   

    参考下他的代码吧,调度管理,资源释放等
    http://hi.baidu.com/pladxm/blog/item/28e7d18217e017a10df4d24b.html
      

  5.   


    如下为要求,需要注意些什么? 
      JVM最大内存 80 MB 
      JSP空间磁盘总量 500 MB 
      WEB每秒请求数 50 
      数据库最大连接数 15 
      

  6.   

      JVM最大内存 80 MB ----tomcat/bin/catalina.sh设置一下就可以。。
      JSP空间磁盘总量 500 MB 
      WEB每秒请求数 50 apache 设置一下。
      数据库最大连接数 15  连接池设置一下。。