Giter Club home page Giter Club logo

slickpoolapp's Introduction

Overview

Special shoutout to stefan-_ and rossabaker on IRC for helping to get this working. Those guys seriously rock and took the time to help get this working.

We needed a way to use Slick to accomplish a few things

  1. We wanted a trait extended by ScalatraBootstrap that would globally setup and tear down the connectionpool for 2 sets of DB clusters. One for our master DB and one for our slave DB VIp

  2. We needed a clean way to share that across controllers

  3. We needed to make sure we were not re-creating connection pools for each servlet

Assumptions to get this demo working

For our tests we setup a sample users table in 2 different databases and then pointed each data source in c3p0-config.xml to use a different one to simulate connecting to different databases.

We modeled out a users table with a single email field just as a test. Modify SlickSupport.scala so that it suits your DB needs by changing the model name and the fields in the table. You can then update the getUsers method in SlickSupport and the controller to reference whatever methods you need from your "model". This is just an example of how we think this can work.

Where should I look ?

SlickInit.scala

A trait extended just by ScalatraBootstrap which is used to initialize the connection pool. All this does is create 2 vals that each hold a reference to the db object that get passed to the contructor of your servlet classes. This trait is to hold vals/defs needed for startup and shutdown of the app

SlickSupport.scala

Here you can put your support methods to help with any database queries. Not entirely necessary, but it certainly helps abstract away "model" behaviours into a trait out of your controller

c3p0-config.xml

Here you can configure multiple data sources that SlickInit will read and connect to. This allows easy configuration of the DataSources by name.

ScalatraBootstrap.scala

Check out here how we create a val for each DB instance that we need and then pass those to the constructor of the servlets. Also note that we needed to add the destroy method to ensure we tear down the connection pool on app shutdown

MyScalatraServlet

Sample controller servlet that you would use. Notice how we extend the SlickSupport trait and also notice how we add constructor params to the Servlet. This allows us to pass in an instance of the db vals from the Bootstrap file to obtain a DB Session

slickpoolapp's People

Contributors

denen99 avatar

Stargazers

Sunghyouk Bae avatar SwhGo_oN avatar Will Sargent avatar Sameer avatar Kai(luo) Wang avatar  avatar Massimiliano Mazzarolo avatar Ross A. Baker avatar Kyle Fitch avatar Pedro Furlanetto avatar

Watchers

 avatar James Cloos avatar Sunghyouk Bae avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.