Transaktionen Spring

  • Themenstarter Themenstarter Gast2
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
Implementierung testen kann man aber auch, wenn man gegen das Interface arbeitet. Das Interface allein macht ja nichts und es hat den Vorteil, du kannst die gleiche Testklasse für verschiedene Implementierungen nutzen.
 
Implementierung testen kann man aber auch, wenn man gegen das Interface arbeitet. Das Interface allein macht ja nichts und es hat den Vorteil, du kannst die gleiche Testklasse für verschiedene Implementierungen nutzen.

Ja das ist was ich oben mit praktischer gemeint hab 😀...
Versteh nicht ganz was WireByTyp macht?
 
"Searches a bean with class that implements the needed InterfaceType."

Funktioniert aber nur, wenn genau ein passendes Bean da ist. Bei mehreren bekommst du eine Fehlermeldung. Alternative WireByName, da wird ein Bean mit Id passend zum Namen des Feldes gesucht oder mit Qualifier, dann kannst du die Id des Beans, welches injected werden soll, angeben.
 
"Searches a bean with class that implements the needed InterfaceType."

Funktioniert aber nur, wenn genau ein passendes Bean da ist. Bei mehreren bekommst du eine Fehlermeldung. Alternative WireByName, da wird ein Bean mit Id passend zum Namen des Feldes gesucht oder mit Qualifier, dann kannst du die Id des Beans, welches injected werden soll, angeben.

Ah okay danke gut zu wissen =)...
Habs jetzt mal mit dem Interface und @Autowired gemacht...
und komm wieder bis zu meinem DAO...
aber sobald ich ein load aufrufe klappts immer noch net

Info
Java:
20.11.2009 19:19:32 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [testContext.xml]
20.11.2009 19:19:33 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericApplicationContext@78aa80: startup date [Fri Nov 20 19:19:33 CET 2009]; root of context hierarchy
20.11.2009 19:19:33 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
INFO: Loading properties file from class path resource [jdbc.properties]
20.11.2009 19:19:33 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
INFO: Bean 'dataSource' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
20.11.2009 19:19:34 org.springframework.orm.hibernate3.LocalSessionFactoryBean buildSessionFactory
INFO: Building new Hibernate SessionFactory
20.11.2009 19:19:35 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
INFO: Bean 'sessionFactory' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
20.11.2009 19:19:36 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@fccada: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,dataSource,sessionFactory,transactionManager,kundenDAO,kundenService]; root of factory hierarchy
20.11.2009 19:19:36 org.springframework.orm.hibernate3.HibernateTransactionManager afterPropertiesSet
INFO: Using DataSource [org.apache.commons.dbcp.BasicDataSource@40a47f] of Hibernate SessionFactory for HibernateTransactionManager
20.11.2009 19:19:36 org.springframework.test.context.transaction.TransactionalTestExecutionListener startNewTransaction
INFO: Began transaction (1): transaction manager [org.springframework.orm.hibernate3.HibernateTransactionManager@1788551]; rollback [true]
20.11.2009 19:20:20 org.springframework.test.context.transaction.TransactionalTestExecutionListener endTransaction
INFO: Rolled back transaction after test execution for test context [[TestContext@5f07e8 testClass = KundenServiceTest, locations = array<String>['classpath:/testContext.xml'], testInstance = kunde.KundenServiceTest@100271a, testMethod = testSerchKunde@KundenServiceTest, testException = org.hibernate.HibernateException: load is not valid without active transaction]]
20.11.2009 19:20:20 org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.GenericApplicationContext@78aa80: startup date [Fri Nov 20 19:19:33 CET 2009]; root of context hierarchy
20.11.2009 19:20:20 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@fccada: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,dataSource,sessionFactory,transactionManager,kundenDAO,kundenService]; root of factory hierarchy
20.11.2009 19:20:20 org.springframework.orm.hibernate3.AbstractSessionFactoryBean destroy
INFO: Closing Hibernate SessionFactory

StackTarce

Java:
org.hibernate.HibernateException: load is not valid without active transaction
	at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)
	at $Proxy19.load(Unknown Source)
	at dao.impl.HibernateKundenDAOImpl.getKundeById(HibernateKundenDAOImpl.java:30)
	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 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
	at $Proxy15.getKundeById(Unknown Source)
	at service.impl.KundenServiceImpl.searchKundeById(KundenServiceImpl.java:44)
	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 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
	at $Proxy16.searchKundeById(Unknown Source)
	at kunde.KundenServiceTest.testSerchKunde(KundenServiceTest.java:23)
	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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
	at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	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)

Ich versteh immer noch nicht was er hier meckert, dass meine properties nicht geeignet ist...

Java:
INFO: Bean 'dataSource' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
 
Zuletzt bearbeitet von einem Moderator:
5.1 Hibernate und Spring: Auszug aus Kapitel 5 Integration anderer Technologien aus dem Buch Hibernate Vielleicht hilft dir das, ich bin mir nicht sicher wie der ExceptionTrnslator auf die Session und Datasource wirkt. Aber die Fehlermeldung bedeutet, dass Spring versucht BeanPostProcessors selbst zu Proxyobjekten zu verwandeln und das klappt nicht, da der Auto-Proxy Mechanismus selber ein BeanPostProcessor ist.

Danke grad mal durchgelesen sieht so aus wie bei mir =(...
Ich verstehs auch grad nicht weil eigentlich steht ja in der Info das er eine Transaktion startet... komisch ???:L...

Den 2ten Teil hab ich nicht ganz verstanden ^^... Er versucht die Exception umzuwandeln das klappt nicht, wegen? Weiß dass es an der Zeile liegt...^^
[XML]
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
[/XML]
 
Zuletzt bearbeitet von einem Moderator:
Also das Problem besteht immernoch ich benutze Hibernate3

Hier hab ich was gefunden mit dem es geht

Java:
public class HibernateKundenDAOImpl implements KundenDAO{

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public Kunde getKundeById(Long id) {
		Session session = SessionFactoryUtils.getSession(sessionFactory, false);
		return (Kunde) session.load(KundeHibernateImpl.class, id);
	}

Aber durch SessionFactoryUtils hab ich wieder eine dirkete abhängigkeit zu spring.

EDIT: Die 2te Lösung war, dass ich in meiner hibernate.cfg.xml
folgende Zeile rausgemacht habe, danach geht es auch =)
[XML]
<!-- Enable Hibernate's automatic session context management
<property name="current_session_context_class">thread</property>-->
[/XML]

Vielen Dank für eure ganze Mühe =)
 
Zuletzt bearbeitet von einem Moderator:
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben