Wednesday, 15 July 2015

java - How to verify Spring + AspecJ Transactions? -



java - How to verify Spring + AspecJ Transactions? -

i'm pretty sure transactions not beingness applied in spring boot application, though using @enabletransactionmanagement(mode=advicemode.aspectj) & have spring-aspects in app.

configuration @configuration public class datastoreconfig{ @bean public datasource datasource(){ ///... } @bean public jdbctemplate jdbctemplate(datasource datasource){ homecoming new jdbctemplate(datasource); } @bean public string databaseschema(){ homecoming "myschema"; } @bean public opensessioninviewinterceptor opensessioninviewinterceptor(sessionfactory sessionfactory){ opensessioninviewinterceptor result = new opensessioninviewinterceptor(); result.setsessionfactory(sessionfactory); homecoming result; } @bean public localcontainerentitymanagerfactorybean entitymanagerfactory(datasource datasource, jpavendoradapter vendoradapter){ localcontainerentitymanagerfactorybean mill = new localcontainerentitymanagerfactorybean(); factory.setjpavendoradapter(vendoradapter); factory.setpackagestoscan( main.class.getpackage().getname() + ".model"); factory.setdatasource(datasource); factory.setjpaproperties(getjpaproperties()); factory.afterpropertiesset(); homecoming factory; } @bean public hibernatejpavendoradapter jpavendoradapter(){ hibernatejpavendoradapter vendoradapter = new hibernatejpavendoradapter(); vendoradapter.setgenerateddl(false); vendoradapter.setdatabaseplatform(postgresql9dialect.class.getname()); homecoming vendoradapter; } @bean public sessionfactory sessionfactory(hibernateentitymanagerfactory emf){ sessionfactoryimpl sf = (sessionfactoryimpl) emf.getsessionfactory(); homecoming sf; } private properties getjpaproperties() { properties props = new properties(); // props.put("hibernate.hbm2ddl.auto", "validate"); props.put("hibernate.ejb.naming_strategy", improvednamingstrategy.class.getname()); props.put("hibernate.enable_lazy_load_no_trans", "true"); props.put("jadira.usertype.autoregisterusertypes", "true"); homecoming props; } @bean public jpatransactionmanager transactionmanager(entitymanagerfactory emf) throws propertyvetoexception { jpatransactionmanager transactionmanager = new jpatransactionmanager(emf); transactionmanager.setrollbackoncommitfailure(true); transactionmanager.setdatasource(datasource()); homecoming transactionmanager; } @bean public namedparameterjdbctemplate namedparameterjdbctemplate(datasource datasource) { homecoming new namedparameterjdbctemplate(datasource); } @bean public persistenceexceptiontranslationpostprocessor exceptiontranslation() { homecoming new persistenceexceptiontranslationpostprocessor(); } } main class @configuration @enabletransactionmanagement(mode = advicemode.aspectj) @componentscan @enableasync @enablescheduling @enableentitylinks @enableaspectjautoproxy @enableapiresources(apiprefix = "") @enablejparepositories(transactionmanagerref = "transactionmanager") @enablehypermediasupport(type = enablehypermediasupport.hypermediatype.hal) @enableconfigurationproperties @enableautoconfiguration @enableglobalmethodsecurity(securedenabled = true, prepostenabled = true) @enablewebsocketmessagebroker @slf4j public class main extends springbootservletinitializer { @override protected springapplicationbuilder configure( springapplicationbuilder application) { homecoming application.sources(main.class); } public static void main(string[] args) { object[] sources = { main.class, tomcatconfig.class // include embedded tomcat... }; configurableapplicationcontext result = springapplication.run(sources, args); } } attempt verify

one of ways trying verify running

@component public class someservice{ @transactional @override protected void tryitout(){ // false boolean declarativetransaction = transactionsynchronizationmanager.isactualtransactionactive(); // throws exception transactionstatus aspect = transactionaspectsupport.currenttransactionstatus(); } }

however, if insert phone call transactionmanager.gettransaction(new defaulttransactiondefinition()); beforehand, declarativetransaction true.

any advice on how set @transactions / verify they're working?

your configuration contradicting itself.

@enabletransactionmanagement(mode = advicemode.aspectj)

means using loadtime or compile time weaving of aspects. judging on configuration aren't using that. if have justice configuration using basic proxies no weaving.just remove advicemode.aspectj , work, don't need @enableaspectjautoproxy applying transactions has nil (it can work without it).

also configuration doing lot not utilize spring boot. have configured lot of things spring boot automatically, manually.

spring boot configures next (just specifying @enableautoconfiguration.

entitymanagerfactory datasource jdbctemplate transaction setup openentitymanagerinview spring web sockets hypermedia support spring info jpa setup

you can remove whole lot of configuration , set properties in application.properties.

if need sessionfactory instead of doing casting suggest utilize of hibernatejpasessionfactorybean.

i suspect next main class still work (with transaction enabled).

@configuration @componentscan @enableasync @enablescheduling @enableautoconfiguration @enableglobalmethodsecurity(securedenabled = true, prepostenabled = true) @enablewebsocketmessagebroker @slf4j public class main extends springbootservletinitializer { @override protected springapplicationbuilder configure( springapplicationbuilder application) { homecoming application.sources(main.class); } public static void main(string[] args) { object[] sources = { main.class, tomcatconfig.class // include embedded tomcat... }; configurableapplicationcontext result = springapplication.run(sources, args); } }

with datastoreconfig class

@configuration public class datastoreconfig{ @bean public factorybean<sessionfactory> sessionfactory(entitymanagerfactory emf){ hibernatejpasessionfactorybean jpasessionfactorybean = new hibernatejpasessionfactorybean(); jpasessionfactorybean.setentitymanagerfactory(emf); homecoming jpasessionfactorybean; } }

and application.properties

spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.improvednamingstrategy spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true spring.jpa.properties.jadira.usertype.autoregisterusertypes=true spring.jpa.database-platform=org.hibernate.dialect.postgresql9dialect

you need add together spring.datasource. properties setup datasource correctly. judging fact had opensessioninviewinterceptor used instead of openentitymanagerinviewinterceptor (or filter). might able remove that.

not sure if al configuration might able remove/trim downwards more. suggest @ spring boot reference guide feeling automatically configured.

java spring spring-boot spring-transactions

No comments:

Post a Comment