Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: scheduling a DAO operation in hibernate

  1. #11
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Defining the transactional annotation isn't enough, you need to tell spring to process it.

    How are you doing transactions elsewhere? With <tx:advice>? It doesn't matter how you do it, as long as you do it correctly

    Please read the following to understand how to implement transactions in Spring...
    http://static.springframework.org/sp...ansaction.html
    Colin Yates
    SpringSource - http://www.springsource.com - Spring Training, Consulting, and Support - "From the Source"
    Please read http://www.springframework.org/documentation
    Co-Author of Expert Spring MVC + Web Flow.

  2. #12
    Join Date
    Aug 2007
    Posts
    7

    Default

    OK, i got it working, i assume my solution is pretty specific to my own context, but ill post it anyway to see if it helps anyone

    Code:
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
          <ref bean="hibernateSessionFactory" />
        </property>
      </bean>
      <bean id="txProxyController" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
        <property name="transactionManager">
          <ref bean="transactionManager" />
        </property>
        <property name="transactionAttributes">
          <props>
            <prop key="*">PROPAGATION_REQUIRED</prop>
          </props>
        </property>
      </bean>
    
    <bean id="taggerDAO" class="com.verilogue.service.dao.TaggerHibernateDAO">
       		<property name="sessionFactory">
       			<ref bean="hibernateSessionFactory"/>
       		</property>			
      </bean>
      
      <bean id="ModelTrainer" parent="txProxyController">
            <property name="target">
                  <ref bean="nonTran_modelTrainer" />
            </property>
      </bean>
          
      <bean id="nonTran_modelTrainer" class="com.verilogue.service.mallet.DefaultModelTrainer">
       		<property name="taggerDAO">
       			<ref bean="taggerDAO"/>
       		</property>
       		<property name="sessionFactory">
          		<ref bean="hibernateSessionFactory" />
        	</property>		
      </bean>


    The call to set the event, passed in the current application context by a spring instanciated class that is applicationContextAware

    Code:
    public void scheduelTraining(ApplicationContext ctx)
    	{
        	TimerTaskExecutor tte = new TimerTaskExecutor();
        	tte.setDelay(10*1000);
        	tte.afterPropertiesSet();
    		(new ModelTrainerExecutor(tte)).train(this.getUID(),ctx);
    	}


    the trainer executor, gets the trainer from spring using the context at runtime(in the future)
    Code:
    public class ModelTrainerExecutor {
    
    	public class RunModelTrainer implements Runnable {
    		private int modelUID;
    		private ApplicationContext ctx;
    		private ModelTrainer trainer;
    
    		
    		public RunModelTrainer(int m, ApplicationContext ctx)
    		{
    			modelUID=m;
    			this.ctx=ctx;
    		}
    		
    		public void run()
    		{	
    			try
    			{
    			trainer=(ModelTrainer)ctx.getBean("ModelTrainer");
    			trainer.setModelUID(modelUID);
    			trainer.train();
    			}
    			catch(Exception ex)
    			{
    				System.out.println("Cause: "+ex.getCause());
    				ex.printStackTrace();
    			}
    		}
    	}
    
      private TaskExecutor taskExecutor;
    
      public ModelTrainerExecutor(TaskExecutor taskExecutor) {
        this.taskExecutor = taskExecutor;
      }
    
      public void train(int uid, ApplicationContext ctx) {
        taskExecutor.execute(new RunModelTrainer(uid,ctx));
      }
    }


    the train method in the actual trainer
    Code:
    public class DefaultModelTrainer 
    extends HibernateTemplate
    implements ModelTrainer{
    
    	private TaggerDAO taggerDAO;
    	private int modelUID;
    	
    	@Transactional()
    	public void train() {
    		//DAO loads and saves using the object graph
    		
    	}
    
    	public int getModelUID() {
    		return modelUID;
    	}
    
    	public void setModelUID(int modelUID) {
    		this.modelUID = modelUID;
    	}
    
    	public TaggerDAO getTaggerDAO() {
    		return taggerDAO;
    	}
    
    	public void setTaggerDAO(TaggerDAO taggerDAO) {
    		this.taggerDAO = taggerDAO;
    	}

    i sincerely appreciate everyones help and patience, thank you
    Dan

  3. #13
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,425

    Default

    Couldn't you wire all of this together with Spring? All you need to do is make whatever bean you want a prototype.
    Last edited by karldmoore; Aug 27th, 2007 at 02:36 PM.
    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.

Posting Permissions

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