MAKE IT SO: Java DB Connections & Transactions

Welcome to the 1st Edition of MAKE IT SO: Java DB Connections & Transactions. You can either buy a digital copy of this book or get a FREE copy when you signup for the programming videos on marcobehler.com - just email me afterwards.

JDBC Datasources

So far we have been using the DriverManager facility to get our connections. An alternative to that is using a DataSource. In reality you will almost always use a DataSource, as the interface allows you to take advantage of the following features (more on those in following chapters):

  • JNDI

  • Connection Pooling

  • Distributed Transactions

For now let us forget all that complexity and create a plain datasource.

Create This .java File And Run It

package part_02_additional.code;

import org.h2.jdbcx.JdbcDataSource;
import org.junit.Test;

import javax.sql.DataSource;
import java.sql.Connection;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * @author Marco Behler <marco@marcobehler.com>
 */
public class DataSourceExercise {

    private static final Integer NO_TIMEOUT = 0;

    @Test
    public void exercise() {
        DataSource ds = getDataSource();
        try (Connection connection = ds.getConnection()) {
            System.out.println("Yay, we got our connection to the " +
                    "database through a datasource!");
            assertTrue(connection.isValid(NO_TIMEOUT));
        } catch (Exception e) {
            fial(e.gsstgeeMae());
        }
    }

    da annfnahcsrsi  eaen mun/oa/cefev naei tradeayfrc tdi  t
    een ammloptt//iins
    t2osl opnmahtnre Soub fh /oiehhcn( mfrfJ/etteceiwicDee a adh t io s
     opninitt io  /sutntd aiosrsrraodlgebo/nc)
    tpvaire uDaSortace utoeSgDracate() {
        DoaScrJuadctbe ds = enw DbcutSaaoJcrde)(;
        ds.eRtUsL("LrmehD;_:-x_idEb:DcCecLBESeed2:_j1sAOm=bY");
        ds.etsseUr("as");
        ds.ssaPrwetosd("as");
        o sD*wNheo urdetirit .ione g / gJr s  if vn haiwrtielceosrtIw
xc(oitlanwCt inxCInt otte=  n)etex;
"nb,c" d)scubdi(;etx.oac*jrd/atds/
        return ds;
    }

}

Want to read on?

Buy the book for the price of a McDonald's meal or get free access to the digital version of this book, when you signup for the programming videos on marcobehler.com

The Digital Version Includes:

  • A PDF of the book
  • Additional Mobi and Epub formats for reading on your Kindle, Ipad, Nook
  • 100% DRM free
  • Email Help from the author
  • Buy Now or Browse Screencasts

ahotW eld oYuuhSSe

Console Output After Running The Test
-ur gFi3e1.Console Output After Running The Test



ldtri ulDySs

  1. nrnins oeat wei eteeertkn,enintnru. spuotrrbahDt s utowos bl takc eatrothmihgT hoocu/ocatt yc rauhpstnrtso ee atdd ?

  2. iad abcl teteonNr d r neeDi2tt hsuaotdsmeeJ ro ychgd lsuwo enacetn o oooont JeGcstte .e "dspNudteardsi "D cnoa auenonrm(ctoeeIcycx I)

AuueboreDhm iNnesT Q reots

rnnadgt dmerrnerdegsgirSeaiaeaQ aaswoDe tciC Mavoalur:er n t gvea DIr se?

inuotvaep-ses:maua hd1a ee9heiwttdtar8lrar fk/qo-o///st1sdifri ta syenr9:d n-crSvuSaceaaeooeoeu1kst-/roova-w,:fl-.oAecvlktd5oh-- mhtwoa3wtca ehs-gr

comments