Menu

#3 wrapping java.sql.DataSource interface

open
5
2007-04-27
2007-03-08
rafko
No

I cannot see support for wrapping java.sql.DataSource interface :-(
I belive that DataSource is used much offen than DataManager in the WEB applications

Discussion

  • Armando Perdomo

    Armando Perdomo - 2007-04-27

    Logged In: YES
    user_id=1502109
    Originator: NO

    I am missing something in this requirement. Could you give more details about this requirement? What functionalities are you expecting? The DataSource interface has only 6 methods:

    public Connection getConnection() throws SQLException
    public Connection getConnection(String username, String password) throws SQLException
    public PrintWriter getLogWriter() throws SQLException
    public int getLoginTimeout() throws SQLException
    public void setLogWriter(PrintWriter out) throws SQLException
    public void setLoginTimeout(int seconds) throws SQLException

    The datasource wrapper was implemented but I don't see useful information on it. The useful data come from getConnection, but this data can be collected inside the Connection wrapper. Let me know what you think.

    Thanks, Armando.

     
  • Armando Perdomo

    Armando Perdomo - 2007-04-27
    • assigned_to: nobody --> perdom
     
  • Thomas Risberg

    Thomas Risberg - 2007-07-11

    Logged In: YES
    user_id=45649
    Originator: NO

    The way I see it it's an issue of configuration and if you use the DataSource you don't need to worry about the Elvyx specific URL since you can wrap the connection on the DataSource.getConnection call. I made a few modifications to the Driver and DataSource (see attached diffs) so I could configure Elvyx using a regular Spring configuration usig a DataSource and not using the elvyx.properties at all. This is the config where the targetDataSource uses the regular MySQL driver and url and not the Elvyx specific ones:

    <bean id="dataSource" class="com.elvyx.DataSource">
    <constructor-arg index="0" ref="targetDataSource"/>
    <constructor-arg index="1">
    <props>
    <prop key="address">localhost</prop>
    <prop key="port">4448</prop>
    <prop key="version">1.0.23.1b</prop>
    </props>
    </constructor-arg>
    </bean>

    <bean id="targetDataSource"
    class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${jdbc.driverClassName}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="minPoolSize" value="2"/>
    <property name="maxPoolSize" value="15"/>
    <property name="maxStatements" value="50"/>
    </bean>

    So, at least from my perspective, this would make configuration much easier for any application using the Spring Framework's data access features.

     
  • Thomas Risberg

    Thomas Risberg - 2007-07-11

    Logged In: YES
    user_id=45649
    Originator: NO

    Couldn't find an option to attach the diffs, so I'll just paste them here:

    Index: src/main/java/com/elvyx/Driver.java

    RCS file: /cvsroot/elvyx/Elvyx/src/main/java/com/elvyx/Driver.java,v
    retrieving revision 1.2
    diff -u -r1.2 Driver.java
    --- src/main/java/com/elvyx/Driver.java 12 Feb 2007 03:50:32 -0000 1.2
    +++ src/main/java/com/elvyx/Driver.java 11 Jul 2007 14:46:44 -0000
    @@ -24,6 +24,7 @@
    package com.elvyx;

    import java.io.FileInputStream;
    +import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.DriverPropertyInfo;
    @@ -54,8 +55,16 @@
    /** Hostname */
    private static String address = "localhost";

    + private static Properties props = null;
    +
    static {
    - init();
    +// init();
    + try {
    + DriverManager.registerDriver(new Driver());
    + } catch (Exception e) {
    + System.err.println(e.getMessage());
    + throw new RuntimeException("Initialization error", e);
    + }
    }

    public Driver() throws ClassNotFoundException, InstantiationException, IllegalAccessException,
    @@ -66,13 +75,11 @@
    * Load file elvyx.properties
    */
    public synchronized static void init() {
    + if (initialized)
    + return;
    try {
    - if (initialized)
    - return;
    - initialized = true;
    - // Read property file
    - Properties props = new Properties();
    - props.load(new FileInputStream("elvyx.properties"));
    + if (props == null)
    + loadProperties();
    dbDriver = props.getProperty("driver");
    dbUrl = props.getProperty("url");
    port = new Integer(props.getProperty("port"));
    @@ -82,15 +89,28 @@
    // Unregister the driver
    DriverManager.deregisterDriver(driver);
    // Load driver
    - DriverManager.registerDriver(new Driver());
    +// DriverManager.registerDriver(new Driver());
    // DriverManager.registerDriver(driver);
    + initialized = true;
    } catch (Exception e) {
    - System.err.println("Current directory=" + System.getProperty("user.dir"));
    System.err.println(e.getMessage());
    + throw new RuntimeException("Initialization error", e);
    }
    }

    + private static void loadProperties() throws IOException {
    + // Read property file
    + props = new Properties();
    + try {
    + props.load(new FileInputStream("elvyx.properties"));
    + } catch (IOException e) {
    + System.err.println("Error reading \"elvyx.properties\" from current directory " + System.getProperty("user.dir"));
    + throw e;
    + }
    + }
    +
    public Connection connect(String url, Properties info) throws SQLException {
    + init();
    return Factory.getConnection(driver.connect(dbUrl, info));
    }

    @@ -154,4 +174,12 @@
    public void setPort(String port) {
    Driver.port = new Integer(port);
    }
    +
    + public static void setProperties(Properties props) {
    + Driver.props = props;
    + dbDriver = props.getProperty("driver");
    + dbUrl = props.getProperty("url");
    + port = new Integer(props.getProperty("port"));
    + address = props.getProperty("hostname");
    + }
    }

    Index: src/main/java/com/elvyx/DataSource.java

    RCS file: /cvsroot/elvyx/Elvyx/src/main/java/com/elvyx/DataSource.java,v
    retrieving revision 1.1
    diff -u -r1.1 DataSource.java
    --- src/main/java/com/elvyx/DataSource.java 18 Jan 2007 02:08:11 -0000 1.1
    +++ src/main/java/com/elvyx/DataSource.java 11 Jul 2007 14:48:39 -0000
    @@ -26,6 +26,7 @@
    import java.io.Serializable;
    import java.sql.Connection;
    import java.sql.SQLException;
    +import java.util.Properties;

    /**
    * DataSource wrapper
    @@ -38,17 +39,22 @@
    public class DataSource implements javax.sql.DataSource, Serializable {

    private static final long serialVersionUID = 1L;
    - private DataSource dataSource = null;
    + private javax.sql.DataSource dataSource = null;

    public DataSource() {
    }

    - public DataSource(DataSource dataSource) {
    + public DataSource(javax.sql.DataSource dataSource) {
    this.dataSource = dataSource;
    }

    + public DataSource(javax.sql.DataSource dataSource, Properties props) {
    + this.dataSource = dataSource;
    + Driver.setProperties(props);
    + }
    +
    public Connection getConnection() throws SQLException {
    - return dataSource.getConnection();
    + return Factory.getConnection(dataSource.getConnection());
    }

    public Connection getConnection(String username, String password) throws SQLException {

     

Log in to post a comment.