J and I and Me
2011-11-01
  Spring and Scala
Scala is an interesting language that appeals to a lot of Java developers as it is statically typed - just like Java. However, Scala focuses on concurrent processing. The choice of frameworks for typically bread and butter issues that you see in Enterprise applications is rather limited. For that reason it makes sense to look at Spring as a very mature and established Java Enterprise technology and whether it can be used with Scala. So here is a presentation and some sample code:
Scala and Spring
View more presentations from Eberhard Wolff
https://github.com/ewolff/scala-spring

Labels: ,

  11:14
Bookmark and Share
Comments:
Thanks for the nice presentation of Scala & Spring playing in concert.

On slide 39 you describe how transactions could be imposed on code blocks via currying and implicit conversions. You mention that it is not possible to make a whole class transactional, but that's not true. You just have to use some more of Scala powerful concepts, i.e. implicit parameters and values in this case.

Look at the following modification:

scala> class Foo {
| def bar[A](block: => A)(implicit tx: Boolean = false): A = {
| println(tx) // Transaction magic happens here ...
| block
| }
| }
defined class Foo

Now you can call bar without tx:

scala> foo.bar(1)
false
res0: Int = 1

Or with explicit tx:

scala> foo.bar(1)(true)
false
res0: Int = 1

And you could define an implicit value for the tx in the class:

scala> class Foo {
| implicit val defaultTx = true
| def bar[A](block: => A)(implicit tx: Boolean = false): A = {
| println(tx) // Transaction magic happens here ...
| block
| }
| }
defined class Foo

Then all methods defined like bar will be transactional by default:

scala> class Foo {
| implicit val defaultTx = true
| def bar[A](block: => A)(implicit tx: Boolean = false): A = {
| println(tx)
| block
| }
| }
defined class Foo

scala> val foo = new Foo
foo: Foo = Foo@6d347991

scala> foo.bar(1)
true
res6: Int = 1
 
Thanks for the nice presentation of Scala & Spring playing in concert.

On slide 39 you describe how transactions could be imposed on code blocks via currying and implicit conversions. You mention that it is not possible to make a whole class transactional, but that's not true. You just have to use some more of Scala powerful concepts, i.e. implicit parameters and values in this case.

Look at the following modification:

scala> class Foo {
| def bar[A](block: => A)(implicit tx: Boolean = false): A = {
| println(tx) // Transaction magic happens here ...
| block
| }
| }
defined class Foo

Now you can call bar without tx:

scala> foo.bar(1)
false
res0: Int = 1

Or with explicit tx:

scala> foo.bar(1)(true)
false
res0: Int = 1

And you could define an implicit value for the tx in the class:

scala> class Foo {
| implicit val defaultTx = true
| def bar[A](block: => A)(implicit tx: Boolean = false): A = {
| println(tx) // Transaction magic happens here ...
| block
| }
| }
defined class Foo

Then all methods defined like bar will be transactional by default:

scala> class Foo {
| implicit val defaultTx = true
| def bar[A](block: => A)(implicit tx: Boolean = false): A = {
| println(tx)
| block
| }
| }
defined class Foo

scala> val foo = new Foo
foo: Foo = Foo@6d347991

scala> foo.bar(1)
true
res6: Int = 1
 
Hi Heiko,

thanks for the feedback!

If you look at Spring Tx support with Spring AOP and annotations you can use

class CustomerDAO {
@Transactional
def aMethod() = {}
}

This is very similar to the higher order function Scala provides:

class CustomerDAO {
@Transactional
def aMethod() = transactional() {}
}

What I am missing is an equivalent to

@Transactional
class CustomerDAO {
def aMethod() = {}
}

This way each method is transactional and there is no impact on the code of the methods. The solution you are suggesting would still need modifications to each method.
 
Hi Eberhard,

I was misinterpreting your slides, thought you wanted to set a certain propagation value once for all methods. This could easily be achieved using an implicit value which is the equivalent to Spring's class level annotation.

But of course you are right: You still need to wrap your method implementations in the transactional-call.

Heiko
 
Kommentar veröffentlichen

<< Home
J for Java | I for Internet, iMac, iPod and iPad | Me for me

ARCHIVES
Juni 2005 / Juli 2005 / August 2005 / September 2005 / Oktober 2005 / November 2005 / Dezember 2005 / Januar 2006 / Februar 2006 / März 2006 / April 2006 / Mai 2006 / Juni 2006 / Juli 2006 / August 2006 / September 2006 / Oktober 2006 / November 2006 / Dezember 2006 / Januar 2007 / Februar 2007 / März 2007 / April 2007 / Mai 2007 / Juni 2007 / Juli 2007 / August 2007 / September 2007 / Oktober 2007 / November 2007 / Dezember 2007 / Januar 2008 / April 2008 / Mai 2008 / Juni 2008 / August 2008 / September 2008 / November 2008 / Januar 2009 / Februar 2009 / März 2009 / April 2009 / Mai 2009 / Juni 2009 / Juli 2009 / August 2009 / September 2009 / Oktober 2009 / November 2009 / Dezember 2009 / Januar 2010 / Februar 2010 / März 2010 / April 2010 / Mai 2010 / Juli 2010 / August 2010 / Oktober 2010 / Januar 2011 / Februar 2011 / März 2011 / April 2011 / Mai 2011 / Juni 2011 / August 2011 / September 2011 / November 2011 / Februar 2012 / April 2012 / Mai 2012 / April 2013 / Mai 2013 / Juni 2013 / Januar 2015 / Juli 2015 / Februar 2016 /

Links

Twitter
Google +
Slideshare
Prezi
XING
LinkedIn
Das Spring Buch


Feeds

Feedburner


Impressum
Betreiber und Kontakt:
Eberhard Wolff
Leobschützer Strasse 22
13125 Berlin
E-Mail-Adresse: eberhard.wolff@gmail.com

Verantwortlich für journalistisch-redaktionelle Inhalte:
Eberhard Wolff