0

How could I convert the following into scala.

public class JedisDB {
  private static final JedisPool jedisPool = new JedisPool(getJedisPoolConfig());


  public static JedisPoolConfig getJedisPool() {
      // ..
  }

  public int getTest123() {
         jedisPool.getResource();
         // code goes here
  }
}

I have seen answers do create a class and a companion object, but can someone explain to me exactly how and why I should do this?

Should I create what I want to expose as a static variable in the companion object, and the loading of the configuration file that is used to initialize the jedisPool in the class?

Do I have the option of making jedisPool public or private in the companion object?

Also (not to effect the answer to my question but as a added benefit), I read somewhere but didn't fully understand that this makes pattern makes testing difficult, are there workarounds then?

Blankman
  • 259,732
  • 324
  • 769
  • 1,199

2 Answers2

0
lazy val jedisPool : JedisPool = {
   val poolConfig = createPoolConfig(app)
   new JedisPool(poolConfig)
 }

To get a resource

 val j = jedisPool.getResource()

make sure you return resource after done using it.

 jedisPool.returnResource(j)
Vikas Pandya
  • 1,998
  • 1
  • 15
  • 32
  • I'm looking for what would go in the class and what would go in the companion object, and some explanation around that. – Blankman May 22 '14 at 02:02
  • you don't need a case class in this case. IMO solution I described is simpler and should work. you don't require a case class just for creating jedisPool. that lazy val is a block of code if you notice. – Vikas Pandya May 22 '14 at 03:22
  • 1
    This would be a great opportunity for your `JedisPool` to add some value and implement the [Loan Pattern](http://stackoverflow.com/a/17851535/649048) - making the return of the resource automagical. – millhouse May 22 '14 at 03:32
  • @millhouse yes a great, nah, fantastic opportunity indeed! I was thinking of doing that once I fully understood the companion object idea. – Blankman May 22 '14 at 03:47
0

Basically it does not metter if static methods will go to companion object or any other object. A companion object differs from other objects as it has access rights to the class/trait that other objects do not. But that's not really your's example.

Your sample with companion object:

// -- helpers to be able compile
class JedisPoolConfig { }
class JedisPool(p: JedisPoolConfig) {
  def getResource = 1
}
// --

// everythis that should be SINGLETON goes into object
object JedisDB {
  private lazy val jedisPool = new JedisPool(getJedisPool)
  def getJedisPool = new JedisPoolConfig()   // or any other implementation
  def otherStaticMethod = new JedisDB().anyVal // wow - got access to private val.
}

class JedisDB {
  import JedisDB._
  def  getTest123() = jedisPool.getResource


  private val anyVal = "SomeValue";

  // other methods 

}

// other - non companion object
object JedisDB2 {
  // def otherStaticMethod = new JedisDB().anyVal // no luck - no access
}
vvg
  • 6,325
  • 19
  • 36