Why do we use a DataSource instead of a DriverManager?

前端 未结 5 474
栀梦
栀梦 2020-12-07 07:39

I am reading the Java JDBC specification (vr. 4) and I encountred this statement:

DataSource — this interface was introduced in the JDBC 2.0 Optional

相关标签:
5条回答
  • 2020-12-07 08:08

    Below code shows two way for getting connection.

    There is no need to know about URL in case of mySqlDataSource as this line is commented.

    public class MySqlDataSourceTest {
    
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
    
    
        /************** using MysqlDataSource starts **************/
        MysqlDataSource d = new MysqlDataSource();
        d.setUser("root");
        d.setPassword("root");
    //  d.setUrl("jdbc:mysql://localhost:3306/manavrachna");
        d.setDatabaseName("manavrachna");
        Connection c =  (Connection) d.getConnection();
        /************** using MysqlDataSource ends**************/
    
    
        /************** using DriverManager start **************/
        Class.forName("com.mysql.jdbc.Driver");
        Connection c=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/manavrachna","root","root");
        /************** using DriverManager ends **************/
    
        Statement st=(Statement) c.createStatement();
        ResultSet rs=st.executeQuery("select id from employee");
        while(rs.next())
        {
            System.out.println(rs.getInt(1));
        }
    
    }
    
    }
    
    0 讨论(0)
  • 2020-12-07 08:21

    DriverManager

    • hampers the application performance as the connections are created/closed in java classes.
    • does not support connection pooling.

    DataSource

    • improves application performance as connections are not created/closed within a class, they are managed by the application server and can be fetched while at runtime.
    • it provides a facility creating a pool of connections
    • helpful for enterprise applications
    0 讨论(0)
  • 2020-12-07 08:24

    DataSource objects can provide connection pooling and distributed transactions, so you may have to use DataSource if you need one of or both these features.

    0 讨论(0)
  • 2020-12-07 08:26

    We can get connection using a datasource as follows. Use the connection to perform any database query.

    DataSource datasource = (DataSource) new InitialContext().lookup(dataSourceName);
    Connection connection = datasource.getConnection();
    
    0 讨论(0)
  • 2020-12-07 08:35

    Better scalability and maintenance

    For DriverManager you need to know all the details (host, port, username, password, driver class) to connect to DB and to get connections. Externalizing those in a properties file doesn't change anything about the fact that you need to know them.

    Using a DataSource you only need to know the JNDI name. The AppServer cares about the details and is not configured by the client application's vendor, but by an admin where the application is hosted.

    Scalability:

    Suppose you need to create connections yourself, how would you deal with changing load, sometime you have 10 users sometime you have 1000, you can't just get a connection whenever you need one and later 'release' it so the Database server does not get out of connections, which leads you to connection pooling. DriverManager does not provide it, DataSource does.

    If you are going to program a connection pool yourself then you have to use DriverManager, otherwise go with DataSource.

    0 讨论(0)
提交回复
热议问题