简体   繁体   English

在rmi中将c3p0配置和getConnection方法放在何处

[英]Where to Place c3p0 configuration and getConnection method in rmi

im making a rmi based client server application. 即时通讯基于RMI的客户端服务器应用程序。

At Server side, it consists of an java file running rmi(for binding registries) and all the required interfaces implemeneted invoking other classes meant for various server based operation (which includes a connection with the database). 在服务器端,它由一个运行rmi(用于绑定注册表)的Java文件和所有必需的接口组成,这些接口调用了用于进行各种基于服务器的操作(包括与数据库的连接)的其他类。

Now the doubt is 现在的疑问是

" Where Shall I place the code for configuring ComboPoolDataSource Instance and the getConnection() method so that the configuration can be done when i run the java rmi file and i can invoke .getConnection() from any other java file. 我应该在哪里放置用于配置ComboPoolDataSource实例getConnection()方法的代码,以便在我运行java rmi文件并且可以从任何其他java文件调用.getConnection()时进行配置。

您可以将其放在执行绑定的启动类中,也可以放在单例类中。

At First...Create the code to initiate the connection in a class containing static methods or variables like this the following.. 首先...创建代码以在包含静态方法或变量的类中启动连接,如下所示。

        private static ComboPooledDataSource cpds = new ComboPooledDataSource();
        public static void MakePool()
        {
               try 
               {
                 cpds=new ComboPooledDataSource();
                 cpds.setDriverClass("com.mysql.jdbc.Driver");
                 cpds.setJdbcUrl("jdbc:mysql://localhost:3306/att_db");
                 cpds.setUser("root");
                 cpds.setPassword("dragon");
                 cpds.setMaxPoolSize(MaxPoolSize);
                 cpds.setMinPoolSize(MinPoolSize);
                 cpds.setAcquireIncrement(Accomodation);
             } 
             catch (PropertyVetoException ex) 
             {
                 //handle exception...not important.....
              }

}
public static Connection getConnection()
{
           return cpds.getConnection();
}

Once ur done Create another class meant for server operations.... 完成后,请创建另一个用于服务器操作的类。

and get the Connections from the Pool... 并从池中获取连接...

         try{

               con=DatabasePool.getConnection();
               // DatabasePool is the name of the Class made earlier....
               .
               .
               .
               .  // Server operations as u wanted.....
               .
               .
             }
             catch(SQL EXCEPTION HERE)
             {
                  .....
             }
             finally
             {     
               if(con!=null)
               {
                      con.close();      
               }
             }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM