Results 1 to 6 of 6

Thread: Accessing a BasicDataSource

  1. #1
    Join Date
    Jul 2005
    Location
    Zagreb, Croatia
    Posts
    69

    Default Accessing a BasicDataSource

    Hello everybody....I have a question, probably a silly one to many but here goes...
    This is my databaseContext.xml:

    <bean id="MyDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName">
    <value>${database.driverClassName}</value>
    </property>
    <property name="url">
    <value>${database.url}</value>
    </property>
    <property name="username">
    <value>${database.username}</value>
    </property>
    <property name="password">
    <value>${database.password}</value>
    </property>
    <property name="initialSize">
    <value>${database.datasource.jakarta.dbcp.basicDat aSource.initialSize}</value>
    </property>
    <property name="minEvictableIdleTimeMillis">
    <value>${database.datasource.jakarta.dbcp.basicDat aSource.minEvictableIdleTimeMillis}</value>
    </property>
    <property name="testOnBorrow">
    <value>${database.datasource.jakarta.dbcp.basicDat aSource.testOnBorrow}</value>
    </property>
    <property name="timeBetweenEvictionRunsMillis">
    <value>${database.datasource.jakarta.dbcp.basicDat aSource.timeBetweenEvictionRunsMillis}</value>
    </property>
    <property name="validationQuery">
    <value>${database.datasource.jakarta.dbcp.basicDat aSource.validationQuery}</value>
    </property>
    </bean>


    How do I access the BasicDataSource from within java code??
    Could anybody help me?
    Thanks in advance

  2. #2
    Join Date
    Aug 2004
    Location
    Carlisle, UK
    Posts
    184

    Default

    You inject it as a dependency into objects that need it.

    In other words, you have an object with a DataSource property called, say, dataSource, and a setter for it.

    In your Spring config file, you configure this object as a bean, and set the DataSource property to the MyDataSource bean:
    Code:
    <bean id="beanThatNeedsADataSource" class="x.y.z.MyClass">
        <property name="dataSource"><ref bean="MyDataSource"/></property>
        . . . set other properties
    </bean>

    HTH
    Chris Harris
    Carlisle, UK

  3. #3
    Join Date
    Aug 2004
    Location
    Carlisle, UK
    Posts
    184

    Default

    I guess I should also say you can get at the bean programmatically - something like:
    Code:
    ApplicationContext ctx = new ClassPathXmlApplicationContext&#40;"databaseContext.xml"&#41;;
    DataSource ds = &#40;DataSource&#41;ctx.getBean&#40;"MyDataSource"&#41;;
    But you don't want to be doing that sort of stuff if you don't need to.
    Chris Harris
    Carlisle, UK

  4. #4
    Join Date
    Jul 2005
    Location
    Zagreb, Croatia
    Posts
    69

    Default

    This is the most of my bean:
    public class IzracunNajmaStoredProcedure extends StoredProcedure {

    public static final String m_SQL = "Izracun_Zast_Najamnina";
    private Map m_map;
    private DataSource m_dataSource;



    /**
    * @return Returns the dataSource.
    */
    public DataSource getDataSource() {
    return m_dataSource;
    }
    /**
    * @param dataSource The dataSource to set.
    */
    public void setDataSource(DataSource dataSource) {
    m_dataSource = dataSource;
    }
    /**
    * @return Returns the map.
    */
    public Map getMap() {
    return m_map;
    }
    /**
    * @param map The map to set.
    */
    public void setMap(Map map) {
    m_map = map;
    }
    public IzracunNajmaStoredProcedure() {

    setDataSource(getDataSource());
    setFunction(false);
    setSql(m_SQL);

    declareParameter(new SqlReturnResultSet("resultsList", new RowMapper()
    {

    public Object mapRow(final ResultSet p_resultSet, int p_row) throws SQLException {
    BigDecimal p_L = null;
    BigDecimal p_Ki = null;
    BigDecimal p_Nz = null;
    BigDecimal p_Nz_min = null;
    BigDecimal p_Nz_p_Nz_min = null;

    Map map = new HashMap(10);

    p_L = p_resultSet.getBigDecimal(1);
    p_Ki = p_resultSet.getBigDecimal(2);
    p_Nz = p_resultSet.getBigDecimal(3);
    p_Nz_min = p_resultSet.getBigDecimal(4);
    p_Nz_p_Nz_min = p_resultSet.getBigDecimal(5);
    map.put("p_L",p_L);
    map.put("p_Ki",p_Ki);
    map.put("p_Nz",p_Nz);
    map.put("p_Nz_min",p_Nz_min);
    map.put("p_Nz/p_Nz_min",p_Nz_p_Nz_min);

    return map;
    }
    ...
    ...
    ..


    This is the dbContext:
    <bean id="IzracunNajmaStoredProcedure"
    class="hr.morh.stanovi.business.storedProcedureBea nsImpl.IzracunNajmaStoredProcedure"
    >
    <property name="dataSource">
    <ref bean="MyDataSource" />
    </property>
    </bean>


    This is the exception i get when i try to run tomcat:
    org.springframework.dao.InvalidDataAccessApiUsageE xception: dataSource is required
    at org.springframework.jdbc.object.RdbmsOperation.com pile(RdbmsOperation.java:269)
    at hr.morh.stanovi.business.storedProcedureBeansImpl. IzracunNajmaStoredProcedure.<init>(IzracunNajmaSto redProcedure.java:105)

    I'm a bit confused as to what's going on

  5. #5
    Join Date
    Aug 2004
    Location
    Carlisle, UK
    Posts
    184

    Default

    Well I could be wrong, but it looks to me as if you're overriding the setDataSource() method of StoredProcedure with your own method.

    When writing setters & getters, it's usual to use the property name, in other words, call your setter setM_dataSource() instead of setDataSource(), and your getter getM_dataSource() instead of getDataSource().

    Then, in your IzracunNajmaStoredProcedure() method, change the line
    setDataSource(getDataSource());
    to
    setDataSource(getM_dataSource());
    and that line will call the setDataSource() of StoredProcedure, instead of the one you wrote.

    HTH
    Chris Harris
    Carlisle, UK

  6. #6
    Join Date
    Jul 2005
    Location
    Zagreb, Croatia
    Posts
    69

    Default

    Solved it already, thanks to you, guru cmgharris...Thanks a bunch

Similar Threads

  1. Replies: 8
    Last Post: Jun 12th, 2007, 01:45 PM
  2. Accessing Service Objects in Actions
    By curtney in forum Web Flow
    Replies: 8
    Last Post: Dec 29th, 2006, 02:55 AM
  3. Accessing two databases, one readonly
    By aaime in forum Data
    Replies: 1
    Last Post: May 19th, 2005, 08:46 AM
  4. Replies: 5
    Last Post: Apr 14th, 2005, 06:26 AM
  5. Replies: 3
    Last Post: Mar 31st, 2005, 01:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •