Re: Connections & Pooling

From: Mike Kienenberger (mkienen..mail.com)
Date: Thu Dec 01 2005 - 11:07:56 EST

  • Next message: Andrus Adamchik: "Re: relationship to non-class object"

    Noban,

    I don't have the answer to most of your questions.
    However, once you get a DataSource Connection, you need to wrap the
    rest of your code in a try finally block:

        Connection connection = [...].getConnection();;

        try
        {
                // your code here using the data source connection
        }
        finally
        {
            try
            {
                connection.close();
            }
            catch (SQLException exception)
            {
                // handle error or perhaps just ignore it
            }
        }

    On 12/1/05, noban <noban@yandex.ru> wrote:
    > Looks like I have problems in loading configuration, or in my checkConnection() method
    >
    > So I have 2 questions:
    >
    > 1. What is the right way to check connection (e.g. can we process QUERRIES, is DB alive or NOT) ?
    >
    > 2. What is the right way to connect to the DB with provided login password. Sorry Andrus, I remember we had talk about it, but I can`ty find that info.
    >
    > My solutions (almost sure - they are wrong/ugly):
    > 1. To check connection :
    >
    > try {
    > DataSource ds =
    > Configuration.getSharedConfiguration().getDomain()
    > .getNode(Messages.getString("LoginManager.DomainName")) //$NON-NLS-1$
    > .getDataSource();
    >
    > Connection con = ds.getConnection();
    >
    > if (!con.isClosed()) {
    > return ResourceLogin.CONNECTION_RETURN_CONNECTED;
    > } else {
    > return ResourceLogin.CONNECTION_RETURN_DISCONNECTED;
    > }
    > } catch (SQLException e) {
    > ..... // Process errorr msg and find cause: LOGIN/PSW - wrong, DB server downed, etc.
    >
    > 2. I`m loading configuration, but after that setting login and password to the datasource:
    >
    > Thread.currentThread().setContextClassLoader(
    > this.
    > getClass().
    > getClassLoader()
    > );
    >
    >
    >
    > DefaultConfiguration conf = new DefaultConfiguration();
    > conf.addClassPath(ResourceLogin.CAYENNE_XML_CONFIGURATIUON_PATH);
    > Configuration.initializeSharedConfiguration(conf);
    >
    > Configuration.bootstrapSharedConfiguration(this.getClass());
    >
    > DataSource dataSource;
    >
    > try {
    >
    > dataSource =
    > new PoolManager("net.sourceforge.jtds.jdbc.Driver", //$NON-NLS-1$
    > Messages.getString("LoginModel.jdbcConnectParam"), //$NON-NLS-1$
    > ResourceLogin.DB_MIN_CONNECTIONS,
    > ResourceLogin.DB_MAX_CONNECTIONS,
    > login,
    > password);
    >
    > Configuration config = Configuration.getSharedConfiguration();
    >
    > DataDomain domain = config.getDomain();
    > DataNode node = domain.getNode(Messages.getString("LoginManager.DomainName")); //$NON-NLS-1$
    > node.setDataSource(dataSource);
    >
    > return Configuration.getSharedConfiguration().getDomain()
    > .createDataContext();
    > } catch (SQLException e) {
    > e.printStackTrace();
    >
    > return null;
    > }
    > }
    >
    > As well it is good to say that everething this is Eclipse RCP application, so
    > cayenne.jar is packed as separate plugin (Therefore I`m setting ContextClassLoader).
    >
    >
    > >I am not sure if you've followed this recent thread, and tried any of
    > >the suggestions:
    > >
    > >http://objectstyle.org/cayenne/lists/cayenne-user/2005/11/0168.html
    > >
    > >In short - two likely causes of exhausted connection pool are many
    > >slow queries being run in parallel or users working at the JDBC (or
    > >Cayenne ResultIterator) level forgetting to close the connections
    > >explicitly.
    > >
    > >Andrus
    > >
    > >
    > >On Nov 30, 2005, at 5:57 PM, noban wrote:
    > >> Hi!
    > >>
    > >> I`m recieving strange error.
    > >>
    > >> After 10 successfull inserts into DB my program recieves error,
    > >> that I`ve reached max connections count, as you can imagine my max
    > >> connections count param is set to 10.
    > >> So my gues is that I`m just wrongly using Cayenne for desktop client.
    > >>
    > >> I`m using it in such a way:
    > >>
    > >> ...
    > >> DataContext dataContext = MyConnectionModel.getDataContext();
    > >> MyTableClass tableClassObj = (MyTableClass)
    > >> dataContext.createAndRegisterNewObject(MyTableClass.class);
    > >> tableClassObj .setColumn1Value(value);
    > >> tableClassObj .setColumn2Value(value);
    > >> ...
    > >> try {
    > >> dataContext.commitChanges();
    > >> } catch (Exception e) {
    > >> e.printStackTrace();
    > >> }
    > >>
    > >> Is this right way to do things?
    > >>
    > >> I mean taht, my program opens all 10 connections, and thats, all.
    > >> What I should do to prevent from opening new connections, and
    > >> how to use opened one`s, or at least close them after:
    > >> ctxt.commitChanges()?
    > >>
    > >> And actually how I can check my hypothesis (that program is opening
    > >> new connections and holding old one`s)?
    > >>
    > >
    >
    >
    > --
    > Сегодня удачный день, чтобы завести почту на Яндексе http://mail.yandex.ru
    >



    This archive was generated by hypermail 2.0.0 : Thu Dec 01 2005 - 11:07:59 EST