2016-12-03 1 views
0

내가 JDBC의 트랜잭션 매니저를 config (설정),하지만 내가 할 때 :jdbc 용 스프링 트랜잭션 관리자가 작동하지 않는 이유는 무엇입니까?

dao.insert(something); 
throw new UnsupportedOperationException(); 

트랜잭션 그것은 간단한 예제

<aop:aspectj-autoproxy /> 

<!-- define JDBC datasource by DBCP --> 
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost:3306/ts" /> 
     <property name="username" value="root" /> 
     <property name="password" value="1234" /> 
    </bean> 

    <!-- DataSourceTransactionManager for jdbc --> 
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
     <property name="dataSource" ref="dataSource"/> 
    </bean> 

    <bean id="fooDao" class="com.oolong.dao.FooDao"> 
     <property name="dstm" ref="txManager"></property> 
    </bean> 

    <bean id="fooService" class="com.oolong.service.DefaultFooService"> 
     <property name="fooDao" ref="fooDao"></property> 
    </bean> 

    <tx:advice id="txAdvice" transaction-manager="txManager"> 
     <tx:attributes> 
      <tx:method name="get*" read-only="true" propagation="REQUIRED"/> 
      <tx:method name="*" read-only="false" propagation="REQUIRED" 
       rollback-for="java.lang.RuntimeException"/> 
     </tx:attributes> 
    </tx:advice> 

    <aop:config> 
     <aop:pointcut id="fooServiceOperation" expression="execution(* com.oolong.service.FooService.*(..))"/> 
     <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/> 
    </aop:config> 

을 롤백하지 않습니다하는 DefaultFooService는 FooService 인터페이스를 구현합니다. 서비스에는 DAO가 있고, FooDao에는 메소드가 테이블에 레코드를 삽입합니다.

코드를 실행할 때. 거래가 생성되지 않았습니다! 물론 롤백 할 수는 없습니다.

FooService 인터페이스

package com.oolong.service; 

import com.oolong.model.Foo; 

public interface FooService { 

    Foo getFoo(String fooName); 

    Foo getFoo(String fooName, String barName); 

    void insertFoo(Foo foo); 

    void updateFoo(Foo foo); 
} 

DefaultFooService.java

package com.oolong.service; 

import com.oolong.dao.FooDao; 
import com.oolong.model.Foo; 

public class DefaultFooService implements FooService { 

    private FooDao fooDao; 

    public void setFooDao(FooDao fooDao) { 
     this.fooDao = fooDao; 
    } 

    public Foo getFoo(String fooName) { 

     throw new UnsupportedOperationException(); 
    } 

    public Foo getFoo(String fooName, String barName) { 
     throw new UnsupportedOperationException(); 
    } 

    public void insertFoo(Foo foo) { 
     fooDao.insert(); 
     throw new UnsupportedOperationException(); 
    } 

    public void updateFoo(Foo foo) { 
     throw new UnsupportedOperationException(); 
    } 
} 

FooDao.java

package com.oolong.dao; 

import java.sql.Connection; 
import java.sql.SQLException; 
import java.sql.Statement; 

import javax.sql.DataSource; 

import org.springframework.jdbc.datasource.DataSourceTransactionManager; 

public class FooDao { 

    private DataSourceTransactionManager dstm; 

    public void setDstm(DataSourceTransactionManager dstm) { 
     this.dstm = dstm; 
    } 

    public void insert() { 
     try { 
      DataSource ds = dstm.getDataSource(); 
      Connection conn = ds.getConnection(); 

      String sql = "insert into item (wid, answer, isCorrect, choiceWid) " 
        + "values ('1', '1', 1, '1')"; 

      Statement st = conn.createStatement(); 
      st.executeUpdate(sql); 

     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

AppContainer.java

public class AppContainer { 

    public static void main(final String[] args) throws Exception { 
     ApplicationContext ctx = new ClassPathXmlApplicationContext("/beans.xml", AppContainer.class); 
     FooService fooService = (FooService) ctx.getBean("fooService"); 
     fooService.insertFoo(new Foo()); 
    } 
} 
01,235,

정보의 log4j 출력은 이전에 UnsupportedOperationException를 throw :

Loaded NamespaceHandler mappings: {http://www.springframework.org/schema/p=org.springframework.beans.factory.xml.SimplePropertyNamespaceHandler, http://www.springframework.org/schema/mvc=org.springframework.web.servlet.config.MvcNamespaceHandler, http://www.springframework.org/schema/util=org.springframework.beans.factory.xml.UtilNamespaceHandler, http://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler, http://www.springframework.org/schema/websocket=org.springframework.web.socket.config.WebSocketNamespaceHandler, http://www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler, http://www.springframework.org/schema/oxm=org.springframework.oxm.config.OxmNamespaceHandler, http://www.springframework.org/schema/jdbc=org.springframework.jdbc.config.JdbcNamespaceHandler, http://www.springframework.org/schema/cache=org.springframework.cache.config.CacheNamespaceHandler, http://www.springframework.org/schema/c=org.springframework.beans.factory.xml.SimpleConstructorNamespaceHandler, http://www.springframework.org/schema/tx=org.springframework.transaction.config.TxNamespaceHandler, http://www.springframework.org/schema/jms=org.springframework.jms.config.JmsNamespaceHandler, http://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandler, http://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler, http://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler} 
Creating shared instance of singleton bean 'org.springframework.aop.config.internalAutoProxyCreator' 
Creating instance of bean 'org.springframework.aop.config.internalAutoProxyCreator' 
Eagerly caching bean 'org.springframework.aop.config.internalAutoProxyCreator' to allow for resolving potential circular references 
Finished creating instance of bean 'org.springframework.aop.config.internalAutoProxyCreator' 
Pre-instantiating singletons in org.s[email protected]3224f60b: defining beans [org.springframework.aop.config.internalAutoProxyCreator,dataSource,txManager,fooDao,fooService,txAdvice,fooServiceOperation,org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0]; root of factory hierarchy 
Returning cached instance of singleton bean 'org.springframework.aop.config.internalAutoProxyCreator' 
Creating shared instance of singleton bean 'dataSource' 
Creating instance of bean 'dataSource' 
Creating shared instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' 
Creating instance of bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' 
Eagerly caching bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' to allow for resolving potential circular references 
Creating instance of bean 'fooServiceOperation' 
Finished creating instance of bean 'fooServiceOperation' 
Finished creating instance of bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' 
Eagerly caching bean 'dataSource' to allow for resolving potential circular references 
Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' 
Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' 
Finished creating instance of bean 'dataSource' 
Creating shared instance of singleton bean 'txManager' 
Creating instance of bean 'txManager' 
Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' 
Eagerly caching bean 'txManager' to allow for resolving potential circular references 
Returning cached instance of singleton bean 'dataSource' 
Invoking afterPropertiesSet() on bean with name 'txManager' 
Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' 
Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' 
Finished creating instance of bean 'txManager' 
Creating shared instance of singleton bean 'fooDao' 
Creating instance of bean 'fooDao' 
Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' 
Eagerly caching bean 'fooDao' to allow for resolving potential circular references 
Returning cached instance of singleton bean 'txManager' 
Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' 
Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' 
Finished creating instance of bean 'fooDao' 
Creating shared instance of singleton bean 'fooService' 
Creating instance of bean 'fooService' 
Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' 
Eagerly caching bean 'fooService' to allow for resolving potential circular references 
Returning cached instance of singleton bean 'fooDao' 
Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' 
Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' 
Creating shared instance of singleton bean 'txAdvice' 
Creating instance of bean 'txAdvice' 
Eagerly caching bean 'txAdvice' to allow for resolving potential circular references 
Returning cached instance of singleton bean 'txManager' 
Creating instance of bean '(inner bean)#63021689' 
Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' 
Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' 
Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' 
Finished creating instance of bean '(inner bean)#63021689' 
Invoking afterPropertiesSet() on bean with name 'txAdvice' 
Finished creating instance of bean 'txAdvice' 
Finished creating instance of bean 'fooService' 
Returning cached instance of singleton bean 'txAdvice' 
Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' 
Returning cached instance of singleton bean 'lifecycleProcessor' 
Returning cached instance of singleton bean 'fooService' 
+0

서버를 시작하거나 시작한 후 서버에 요청을 보낼 때 오류가 발생합니다. – SachinSarawgi

+0

포인트 컷은 FooService가 아닌 DefaultFooService, AFAIK에 있어야합니다. 그러나 훨씬 간단한 방법은 XML보다는 bean 정의와 트랜잭션 빈/메소드, autowiring, Java 설정에 대한 주석을 사용하는 것입니다. –

+0

@SachinSarawgi 그냥 자바 프로젝트, 아니 서버, 아니 요청이야 – Oolong

답변

0

이를 참조하십시오

응용 프로그램 코드는 대신 표준 자바 의 에 DataSourceUtils.getConnection (데이터 소스)를 통해 JDBC 연결을 검색하는 데 필요 EE 형식의 DataSource.getConnection() 호출. 과 같은 스프링 클래스 JdbcTemplate은이 전략을 암시 적으로 사용합니다.

thansk @ JB Nizet, @SachinSarawgi.

JdbcTemplate을 사용해야합니다.