2013-06-05 55 views
0

我有一个性能问题与android的sqlConnexion。问题是,我为服务器上的每个请求打开连接。我宁愿一次打开它,当用户第一次登录第一次创建打开连接,sqlDriver

这里是一些代码:

public void onClick(View v) { 
    // Perform action on click 
    new Thread(new Runnable() { 
     public void run() { 
      Statement statement; 
      try { 
      *//This would be better to instantiate connexion at first...* 
       connexion = DriverManager.getConnection(url,  "login",  
         "pass");//Not true login of course... 
       statement = connexion.createStatement(); 
       ResultSet resultat = statement 
         .executeQuery("SELECT name FROM users;"); 

       while (resultat.next()) { 
        resultId = resultat.getString("name"); 
       } 
      } catch (SQLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }).start(); 

如果我放在DriverManager的方法,我从服务器得到一个错误。 在另一课上,我得到NullPointerException

我知道使用JSON解析器的工作可能是一个更好的形式给出,但我开始学习,和Java/Android的比到足够多得让我headeache现在...

回答

0

不要尝试重用连接!对于每个与数据库的逻辑交互,获取连接,语句或其他任何内容,并确保在函数结束之前关闭它们。然而,为了提高性能,使用诸如Commons BasicDataSource之类的连接“池”。使用这个对你来说是不可见的。您正常使用连接和语句,但BasicDataSource会将这些连接(和PreparedStatements)集中起来以提高性能。

http://commons.apache.org/proper/commons-dbcp/

0

完成下列步骤: 当你声明的Connexion分配为null, 和尝试捕捉检查是否联接为空或不是。 如果你得到空,则获得新的连接,否则不会

connexion = null; 

//In try block 
if(connexion == null) 
    connexion = DriverManager.getConnection(url, "login", "pass"); 
0

谢谢大家的帮助!

我设法解决了这个问题。我做什么,如果这能帮助别人我在这里发布如何:

首先,做出其他CLASSE,对我来说驾驶(),其中我做数据源:

public class Driver implements DataSource { 

    public static Connection connection = null; 
    public static final String url = "jdbc:mysql://url/dataBaseName"; 
    protected String user = "userName"; 
    protected String passwd = "password"; 
     //Andoid related, haven't tested the log so, i don't know if this work. 
    private String state = Environment.getExternalStorageState(); 

    public Driver(){ 
     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
     } catch (ClassNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public PrintWriter getLogWriter() throws SQLException { 
     PrintWriter logWriter = null; 

     if (Environment.MEDIA_MOUNTED.equals(state)) { 
      // We can read and write the media 
      try { 
       logWriter = new PrintWriter(state + "/com.me.appName/Logfile.log"); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     return logWriter; 
    } 

    @Override 
    public int getLoginTimeout() throws SQLException { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public void setLogWriter(PrintWriter arg0) throws SQLException { 
     // TODO Auto-generated method stub 
     if (Environment.MEDIA_MOUNTED.equals(state)) { 
      // We can read and write the media 
      try { 
       DriverManager.setLogWriter(new PrintWriter(
         state + "/com.me.appName/Logfile.log")); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 

    @Override 
    public void setLoginTimeout(int seconds) throws SQLException { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public boolean isWrapperFor(Class<?> arg0) throws SQLException { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public <T> T unwrap(Class<T> arg0) throws SQLException { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public Connection getConnection() throws SQLException { 
     if (connection != null) { 
      System.out.println("Cant create Connection"); 
     } else { 
      connection = DriverManager.getConnection(
        url, user, passwd); 
     } 
     return connection; 
    } 

    @Override 
    public Connection getConnection(String userName, String password) 
      throws SQLException { 
     // TODO Auto-generated method stub 
     if (connection != null) { 
      System.out.println("Cant craete a Connection"); 
     } else { 
      connection = DriverManager.getConnection(
        url, userName, 
        password); 
     } 
     return connection; 
    } 

} 

Finaly在我的主要CLASSE:

中的onCreate

()我实例化驱动程序的话,我可以毫不拖延所需的所有要求:)

public void onClick(View v) { 
       // Perform action on click 
       new Thread(new Runnable() { 
        public void run() { 
         try { 
          Statement statement; 
          //The .getConnection is where i handle the DriverManager. 
          connexion = driver.getConnection(); 
          statement = connexion.createStatement(); 

          ResultSet resultat = statement 
         .executeQuery("SELECT name FROM users;"); 

         while (resultat.next()) { 
          resultId = resultat.getString("name"); 
         } 
         driver.setLoginTimeout(5); 
        } catch (SQLException e1) { 
         // TODO Auto-generated catch block 
         e1.printStackTrace(); 
        } 

       } 
      }).start(); 

这是非常有用的。

再次感谢大家,我认为我无法投票答复您的答案,并希望有一天能够帮助您!