Results 1 to 6 of 6

Thread: very simple spring + hibernate problem (saving to db)

  1. #1
    Join Date
    Aug 2004
    Posts
    22

    Default very simple spring + hibernate problem (saving to db)

    Hi,

    I almost have my first example working-- I have a DAO, my POJO and mapping file and code that uses the DAO. I see that everything works as far as creating objets and storing them in the hibernate session. However when I shut down my DB, none of the data in the hibernate session is actually being saved, when I restart the DB again! Do I need to call session.close or sessionfactory.close myself-- I thought maybe Spring takes care of this?

    Thanks, Jason
    ---
    Code:
    <beans>
    
    	<!-- Datasource that works in any application server
    		You could easily use J2EE data source instead if this were
    		running inside of a J2EE container.
    	 -->
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    		<property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property>
    		<property name="url"><value>jdbc&#58;hsqldb&#58;/Users/novotny/Jakarta/jakarta-tomcat-5.0.28/webapps/gridsphere/WEB-INF/CustomPortal/database/gridsphere</value></property>
    		<property name="username"><value>sa</value></property>
    		<property name="password"><value></value></property>
    	</bean>
    
    	<!-- Hibernate SessionFactory -->
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    		<property name="dataSource"><ref local="dataSource"/></property>
    
    		<!-- Must references all OR mapping files. -->
    		<property name="mappingDirectoryLocations">
    			<list>
    		        <value>WEB-INF/persistence</value>
    			</list>
    		</property>
    
    		<!-- Set the type of database; changing this one property will port this to Oracle,
    			 MS SQL etc. -->
    		<property name="hibernateProperties">
    			<props>
                    <prop key="hibernate.c3p0.max_size">10</prop>
                    <prop key="hibernate.c3p0.min_size">2</prop>
                    <prop key="hibernate.c3p0.timeout">5000</prop>
                    <prop key="hibernate.c3p0.max_statements">100</prop>
                    <prop key="hibernate.c3p0.idle_test_period">3000</prop>
                    <prop key="hibernate.c3p0.acquire_increment">2</prop>
                    <prop key="hibernate.dialect">net.sf.hibernate.dialect.HSQLDialect</prop>
                    <prop key="hibernate.query.substitutions">true 1, false 0, yes 'Y', no 'N'</prop>
                </props>
    		</property>
    	</bean>
    
    	<!-- Pass the session factory to our UserDAO -->
    	<bean id="trackerDao" class="org.gridlab.gridsphere.services.core.tracker.impl.TrackerDaoImpl">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
    </beans>
    -------------

    My TrackerDao class:

    Code:
    public class TrackerDaoImpl extends HibernateDaoSupport implements TrackerDao &#123;
    
        private PortletLog log = SportletLog.getInstance&#40;TrackerServiceImpl.class&#41;;
    
        /**
         * Persists a tracking label to the GridSphere database
         *
         * @param label a label used in identifying the action invoked
         * @param userAgent the user agent string provided by the web browser
         * @param userName the user name
         */
        public void trackURL&#40;String label, String userAgent, String userName&#41; &#123;
            TrackerInfo info = new TrackerInfo&#40;&#41;;
            info.setLabel&#40;label&#41;;
            info.setDate&#40;Calendar.getInstance&#40;&#41;.getTime&#40;&#41;.getTime&#40;&#41;&#41;;
            info.setUserAgent&#40;userAgent&#41;;
            info.setUserName&#40;userName&#41;;
    
            this.getHibernateTemplate&#40;&#41;.saveOrUpdate&#40;info&#41;;
        &#125;
    
        public List getTrackingActions&#40;&#41; &#123;
            return this.getHibernateTemplate&#40;&#41;.find&#40;"from " + TrackerAction.class.getName&#40;&#41; + " as trackeraction"&#41;;
        &#125;
    
        public TrackerAction getTrackingAction&#40;String actionName&#41; &#123;
            List actions = this.getHibernateTemplate&#40;&#41;.find&#40;"from " + TrackerAction.class.getName&#40;&#41; + " as trackeraction where trackeraction.Action=\'" + actionName + "'"&#41;;
            if &#40;&#40;actions != null&#41; && &#40;!actions.isEmpty&#40;&#41;&#41;&#41; &#123;
                return &#40;TrackerAction&#41;actions.get&#40;0&#41;;
            &#125;
            return null;
        &#125;
    
        public void addTrackingAction&#40;TrackerAction action&#41; &#123;
            this.getHibernateTemplate&#40;&#41;.saveOrUpdate&#40;action&#41;;
        &#125;
    
        public void removeTrackingAction&#40;String action&#41; &#123;
            this.getHibernateTemplate&#40;&#41;.delete&#40;action&#41;;
        &#125;
    
        public void clearTrackingActions&#40;&#41; &#123;
            this.getHibernateTemplate&#40;&#41;.delete&#40;"from " + TrackerAction.class.getName&#40;&#41; + " as trackeraction "&#41;;
        &#125;
    
        /**
         * Return a list of the available labels
         *
         * @return a list of the available labels
         */
        public List getTrackingLabels&#40;&#41; &#123;
            return this.getHibernateTemplate&#40;&#41;.find&#40;"select tracker.Label from " +  TrackerInfo.class.getName&#40;&#41; + " as tracker"&#41;;
        &#125;
    
        /**
         * Return a list of TrackerInfo objects for the provided label
         *
         * @return a list of TrackerInfo objects for the provided label
         */
        public List getTrackingInfoByLabel&#40;String label&#41; &#123;
            return this.getHibernateTemplate&#40;&#41;.find&#40;"from " + TrackerInfo.class.getName&#40;&#41; + " as tracker where tracker.Label='" + label + "'"&#41;;
        &#125;
    
    
        public boolean hasTrackingAction&#40;String actionName&#41; &#123;
            TrackerAction ta = getTrackingAction&#40;actionName&#41;;
            return &#40;&#40;ta != null&#41; && &#40;ta.isEnabled&#40;&#41;&#41;&#41;;
        &#125;
    &#125;
    ----

    TrackerInfo.hbm.xml
    -----
    Code:
    <hibernate-mapping>
        <class
            name="org.gridlab.gridsphere.services.core.tracker.impl.TrackerInfo"
            table="tracker_info"
            >
    
            <id
                name="oid"
                column="gsoid"
                type="java.lang.String"
                length="32"
                >
                <generator class="uuid.hex"/>
    
            </id>
    
            <property name="Label" type="string" column="label"/>
            <property name="Date" type="long" column="trackerdate"/>
            <property name="UserAgent" type="string" column="useragent"/>
            <property name="UserName" type="string" column="username"/>
        </class>
    
        <class
            name="org.gridlab.gridsphere.services.core.tracker.impl.TrackerAction"
            table="tracker_action"
            >
    
            <id
                name="oid"
                column="gsoid"
                type="java.lang.String"
                length="32"
                >
                <generator class="uuid.hex"/>
    
            </id>
            <property name="Enabled" type="boolean" column="enabled"/>
            <property name="Action" type="string" column="action"/>
        </class>
    </hibernate-mapping>
    ------

  2. #2

    Default Re: very simple spring + hibernate problem (saving to db)

    how do you mean "I shut down my DB"?

    if you shut down the dbs while the app is running, it may happen, that datas will be not saved. Hibernate doesn't flush the inserted data immediately.

  3. #3
    Join Date
    Aug 2004
    Posts
    22

    Default

    Sorry, it's actually a web application and I'm using hsqlDB. When I shutdown Tomcat and hence the hsqldb the data is lost. I guess what I'm asking is do I need to close the session factory by hand that is configured in the applicationContext descriptor?

    Thanks

  4. #4

    Default

    Quote Originally Posted by novotny
    do I need to close the session factory by hand that is configured in the applicationContext descriptor?

    Thanks
    no.
    aren't you accidentally using hsqldb 'in memory' mode?

  5. #5
    Join Date
    Aug 2004
    Posts
    22

    Default

    I don't think so-- i'm running it in-process mode, running in the same JVM as Tomcat and my application with the actual db specified as:

    Code:
    <property name="url"><value>jdbc&#58;hsqldb&#58;/Users/novotny/Jakarta/jakarta-tomcat-5.0.28/webapps/gridsphere/WEB-INF/CustomPortal/database/gridsphere</value></property>
    It all works fine using my old homegrown PersistenceManager class which creates the Hibernate sessionFactory from my props file and does a close on the session factory as part of the destry() method of my servlet.

    Thanks

  6. #6

    Default

    Quote Originally Posted by novotny
    I don't think so-- i'm running it in-process mode, running in the same JVM as Tomcat and my application with the actual db specified as:

    Code:
    <property name="url"><value>jdbc&#58;hsqldb&#58;/Users/novotny/Jakarta/jakarta-tomcat-5.0.28/webapps/gridsphere/WEB-INF/CustomPortal/database/gridsphere</value></property>
    It all works fine using my old homegrown PersistenceManager class which creates the Hibernate sessionFactory from my props file and does a close on the session factory as part of the destry() method of my servlet.

    Thanks
    inac odkial si? kde pracujes? :-)

Similar Threads

  1. UpgradeAcegi Security System from 0.6.1 to 0.8.3
    By mannobug in forum Security
    Replies: 3
    Last Post: Sep 23rd, 2005, 07:00 PM
  2. Replies: 2
    Last Post: Jul 14th, 2005, 10:50 AM
  3. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM
  4. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  5. Replies: 5
    Last Post: Aug 27th, 2004, 07:13 PM

Posting Permissions

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