Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: DAO Question

  1. #1
    Join Date
    Nov 2007
    Posts
    22

    Default DAO Question

    I am using single DAO (using hibernate) to do all my interactions with the database for an entire web application.

    I'm not completely sure if the approach I am taking will scale. In fact I've seen some problems with too many oracle processes being utilised until it refuses my connections.

    My methods typically look like the following and I'm wondering if anybody could give me some feedback on them... Am I not doing something I should be doing?

    Code:
    public void saveThing(Thing thing) {
            getHibernateTemplate().saveOrUpdate(thing);
    }

    Code:
    public List<Thing> getThings() {
            List loadAll = getHibernateTemplate().loadAll(Thing.class);
            return loadAll;
    }

    The relevant config is as follows...

    Code:
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="annotatedClasses">
    			<list>
    				<value>test.Thing</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
    			</props>
    		</property>
    	</bean>
    
    	<!-- 
    		Declares the DAO - Data Access Object
    	-->
    	<bean id="dao" class="test.dao.MyDAO">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    
    	
      <bean id="dataSource" 
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@XXX:XXX:XXX" />
        <property name="username" value="test" />
        <property name="password" value="test" />
      </bean>

  2. #2
    Join Date
    Sep 2007
    Location
    Oceanside, CA
    Posts
    187

    Default

    Generally speaking, you wouldn't want to use DriverManagerDataSource in a production environment. DriverManagerDataSource does not pool connections. Instead, it creates a new Connection every time one is requested. You should instead consider using a JNDI DataSource provided by the container, or a standalone pooling DataSource implementation such as DBCP or C3P0.
    Mike Bingham

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Aside the usage of the DriverManagerDataSource (as explained by Mike) I don't see any Transaction configuration.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4

    Default

    It is quite straightforward to use the default implementation of DBCP connection pooling; in your datasource bean, use this class: org.apache.commons.dbcp.BasicDataSource (adding commons-dbcp.jar to your application).

  5. #5
    Join Date
    Nov 2007
    Posts
    22

    Default

    I've switched to the DBCP datasource and all seems well. Thanks for your help everybody.

  6. #6
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    C3P0 might be worth keeping in mind if you having any problems with DBCP. A few people have posted here with issues that were solved by switching the to.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  7. #7
    Join Date
    Nov 2007
    Posts
    22

    Default

    I've suddenly discovered that certain patterns of use cause my DAO to block. I tried switching to C3PO from DBCP but I still get the same thing.

    Threads enter the data access/modification methods but do not exit.

    Is there perhaps something wrong with my approach? Is the HibernateTemplate threadsafe?

    Looking at the Oracle database, all the sessions are idle and there are no locks.

    I'm having a slight panic as this is supposed to enter production tomorrow so any help much appreciated.

  8. #8
    Join Date
    Nov 2007
    Posts
    22

    Default

    I think it might be because I am doing this ...
    Code:
    public long getNextSeqVal(String sequenceName) {
            List list = getSession().createSQLQuery("SELECT " + sequenceName + ".NEXTVAL FROM DUAL")
                    .list();
            Object obj = list.get(0);
            return ((BigDecimal) obj).longValue();
        }
    ..and not closing the session. I couldn't see how to execute this query via the HibernateTemplate.

    I'm going to try to figure out how to use the HibernateTemplate for this (any hints appreciated) or just simply close the session when I have finished.

  9. #9
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    HibernateTemplate itself is threadsafe.

    Do you use only HibernateTemplate wrapper methods only or do you create sessions manually at certain places? How do you do transactions? Is your connection pool full? What are the patterns you discovered?

  10. #10
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    Use HibernateTemplate.execute() with a callback to get the hold of the session.

Posting Permissions

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