现在有如下问题一个service类有保存方法一个。
因为在保存的表加了一个唯一键约束。
而我的方法要把这种异常捕捉到进行处理。(ps:先查询后保存这样耗费性能)
现在事务机制是通过hibernate配置文件进行配置的。
现在进行捕捉就是有问题。
现在解决这个问题的前提是不该配置文件,不先查询后保存这样耗费性能得方法。其他的有什么方法么?

解决方案 »

  1.   

    ps:先查询后保存这样耗费性能
    你这个 PS 的结论是怎么得出来的?
      

  2.   


    问题是捕捉了异常,也会报,然后方法本来是返回一个是否成功的一个标示值。到最后方法返回没有结果。

    A testClass
    Main(){
      String a = service.getInfo();//在单元测试的时候 这里没有值
       System.out.println("-----------end-----------");//这里也没有打印}B service{
      String getInfo(){
        return a; //此处debug有值
      }
    }
    org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been ed as rollback-only
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:626)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:314)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy49.createOuterOrders(Unknown Source)
    at TestShopExWebServiceImpl.testCreateShopExSo(TestShopExWebServiceImpl.java:99)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
      

  3.   

    在struts那端trycatch这个service方法,如果有Exception在前台显示message
      

  4.   


    A testClass
    Main(){
      String a = service.getInfo();//在单元测试的时候 a 这里没有返回值
      System.out.println("-----------end-----------");//这里也没有打印}B service{
      String getInfo(){
      return a; //此处debug有值
      }
    }
      

  5.   

    这个问题?
    http://www.blogjava.net/honzeland/archive/2010/02/24/313829.html