Java multiclient server with RMI

35 Views Asked by At

I'm trying to make a multiclient java server for a project school where i can get data or write data on a database(Postgres); i would like to make it with RMI but first of all i don't know if i can make it.

My idea was to make the server class and for every client connection create a thread where i pass to it an istance of the server class, but i think it could consume too many resources. Then,i thought, is it possible to share the same server class instance for every thread? is RMI Thread-safe or should i make it so?

This is only a basic RMI implementation with the database connection statament to connect to it

package RMIServer;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;`your text`


public class TestServer extends UnicastRemoteObject implements Iserver {

    private Statement database = null;

    public TestServer() throws RemoteException {

        super();

        try{
            Class.forName("org.postgresql.Driver");
            Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/data", "postgres", "1234");
            database = con.createStatement();
        }catch(SQLException | ClassNotFoundException e ){
            System.out.println("impossibile stabilire una connessione con il database");
        }

    }

    @Override
    public void request(String username, String password)throws SQLException{

        database.execute("INSERT INTO TEST VALUES("+"'"+username+"',"+"'"+password+"')");

    }
}
0

There are 0 best solutions below