0

I have a transaction manager bean in my xml as follows :

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

and session factory and data source beans as :

<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
    <property name="URL" value="jdbc:oracle:thin:@localhost:1521:XE"/>
    <property name="user" value="user"/>
    <property name="password" value="password"/>
</bean>

<!--   Hibernate SessionFactory -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

with session factory having all the hbm files mapping .

Now I have a UserAddressManagerImpl as follows :

public class UserAddressManagerImpl implements UserAddressManager{
 // methods to read and write in the database
}

and the bean for this is :

<bean id="userAddressManager"   class="com.sodiz.service.impl.UserAddressManagerImpl">  

Now , this UserAddressManagerImpl doesn't have @Transactional on it .

Whenever I make any read operation from this class it works well but when making write operation it fails.

I am using this class packaged in a jar. So I prefer not to change this class.

So, is there any way to perform the read and write operations without using @Transactional annotations?

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
Tarun
  • 271
  • 1
  • 6
  • 18
  • did you tried to use xml configuration? https://stackoverflow.com/questions/36917842/spring-transactional-configuring-xml – T. Jung May 31 '17 at 11:30
  • Simplest way to put an annotation would be to create an empty subclass (or a decorator, or a wrapper, whatever suits your case) with @ Transactionnal at the top, and declare that new class in your application context. That said, not sure what you mean by "reads work but writes fail", and not sure @ Transactionnal would help. Something deeper might be going on... – GPI Feb 22 '19 at 10:56

1 Answers1

0

You would have to use aspects to achieve this without using the annotations:

<bean id="transactionManager" 
   class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="userAddressManager" class="com.sodiz.service.impl.UserAddressManagerImpl"/>

<tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
</tx:advice>

<aop:config>
   <aop:pointcut id="userAddressManagerOperation" 
       expression="execution(* com.sodiz.service.impl.UserAddressManagerImpl.*(..))"/>
   <aop:advisor advice-ref="txAdvice" pointcut-ref="userAddressManagerOperation"/>
</aop:config>

Of course you could use more wilcards so that every service in your package applies to the above transactional settings.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
  • I tried this solution after adding the pom dependancies . Now it is giving me an error of java.lang.VerifyError . Falling of the end of the code. – Tarun May 31 '17 at 12:16
  • make sure you have these defined in your beans tag: xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" – Maciej Kowalski May 31 '17 at 12:18
  • I am already having xmlns:aop="http://www.springframework.org/schema/aop" and xmlns:tx="http://www.springframework.org/schema/tx" in my beans tag – Tarun May 31 '17 at 12:22
  • add the stacktrace please – Maciej Kowalski May 31 '17 at 12:24
  • Caused by: java.lang.VerifyError: (class: com/elcy/integration/custom/service/impl/jaxws_asm/ProcessIncomingFreeTextImportTransaction, method: getArg0 signature: ()Lcom/elcy/integration/custom/service/model/FreeTextImportTransaction;) Falling off the end of the code at java.lang.Class.getDeclaredConstructors0(Native Method) [rt.jar:1.8.0_25] at java.lang.Class.privateGetDeclaredConstructors(Unknown Source) [rt.jar:1.8.0_25] at java.lang.Class.getConstructor0(Unknown Source) [rt.jar:1.8.0_25] at java.lang.Class.getDeclaredConstructor(Unknown Source) – Tarun May 31 '17 at 12:29
  • at org.apache.cxf.common.util.ReflectionUtil$4.run(ReflectionUtil.java:105) at org.apache.cxf.common.util.ReflectionUtil$4.run(ReflectionUtil.java:102) at java.security.AccessController.doPrivileged(Native Method) [rt.jar:1.8.0_25] at org.apache.cxf.common.util.ReflectionUtil.getDeclaredConstructor(ReflectionUtil.java:102) at org.apache.cxf.common.jaxb.JAXBUtils.getValidClass(JAXBUtils.java:582) at org.apache.cxf.jaxb.JAXBContextInitializer.addClass(JAXBContextInitializer.java:309) atorg.apache.cxf.jaxb.JAXBContextInitializer.begin(JAXBContextInitializer.java:187) – Tarun May 31 '17 at 12:30
  • check out this post.. https://stackoverflow.com/questions/100107/causes-of-getting-a-java-lang-verifyerror – Maciej Kowalski May 31 '17 at 12:31