Groovy Sql 사용 Spring 사무 관리

Grails 는 Hibernate 의 동쪽 을 사용 하지 않 으 려 면 Sql 을 직접 사용 할 수 있 습 니 다.
설정 부분 은 참고 할 수 있 습 니 다 -
?
http://www.iteye.com/topic/11506?page=3
?
사용 할 코드: (설정, spring transaction Template, Closure = Callback 과 유사)
?
?
	<bean id="defaultDsTarget" lazy-init="true"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName"><value>${db_local_driver}</value></property>  
		<property name="url"><value>${db_local_url}</value></property>  
		<property name="username"><value>${db_local_user}</value></property>  
		<property name="password"><value>${db_local_pwd}</value></property> 
	</bean>
	<bean id="defaultDs" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
		<constructor-arg>
			<ref bean="defaultDsTarget" />
		</constructor-arg>
	</bean>
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource">
			<ref bean="defaultDsTarget"/>
		</property>
	</bean>

?
?
?
	static DataSource getDs(){
		final dsBeanName = 'defaultDs'
		def context = AppContext.getContextBiz()
		return context.getBean(dsBeanName)
	}

	// use spring platform indenpendent TransactionManager
	static DataSourceTransactionManager getTm(){
		def context = AppContext.getContextBiz()
		return context.getBean('transactionManager')
	}

	// not available in weblogic
	static void withTrans(Closure callback){
		def tm = getTm()
		def transDef = new DefaultTransactionDefinition()
		def status = tm.getTransaction(transDef)
		try {
			callback.call()
			tm.commit(status)
		}catch (ex) {
			log.error('DSManager withTrans failed!', ex)

			tm.rollback(status)
			throw ex
		}
	}

?
			DSManager.withTrans{
				def dao = new YourDAO() // use defaultDs from spring beanfactory
			}

?
?

좋은 웹페이지 즐겨찾기