1 Reply Latest reply on Sep 6, 2006 6:02 PM by Weston M. Price

    How to access the configured datasource

    wjq Newbie

      Hi,I have config a datasource like the following:

      <local-tx-datasource>
      <jndi-name>PostgresDS</jndi-name>
      <connection-url>jdbc:postgresql://localhost:5432/eai</connection-url>
      <driver-class>org.postgresql.Driver</driver-class>
      <user-name>postgres</user-name>
      postgres
      <min-pool-size>5</min-pool-size>
      <max-pool-size>100</max-pool-size>
      <new-connection-sql>select 1</new-connection-sql>
      <check-valid-connection-sql>select 1</check-valid-connection-sql>

      <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->

      <type-mapping>PostgreSQL 7.2</type-mapping>

      </local-tx-datasource>


      Then

      1, how can I get the datasource object so to get a connection ? Since my web app is in the save JVM, could i get the datasource directly without a jndi lookup (may fetch the datasource mbean directly ?)? some code snippet will be really appreciated.

      2,Is the database connections REALLY been pooled ,according the config like above ?

      3,how to config to let jboss server initiate serveral database connection when startup?

      I'm totally new to jboss.I have searched the web for a while but find no result.Anyone can help me ? Thanks!

        • 1. Re: How to access the configured datasource
          Weston M. Price Master

           



          1, how can I get the datasource object so to get a connection ? Since my web app is in the save JVM, could i get the datasource directly without a jndi lookup (may fetch the datasource mbean directly ?)? some code snippet will be really appreciated.



          Being in VM has nothing to do with it. JNDI is the *only* way to look up the managed object. Your choice of how you do this depends:

          You can always look up the object directly in JNDI

          InitialContext initCtx = new InitialContext();
          DataSource ds = (DataSource)initCtx.lookup("java:/PostgresDS");

          Note the use of the "java:/" qualifier in the lookup. DataSources are, by default, bound into the java: namespace and are meant to be used within the context of JBoss and not remotely (ie by a thin Client). Note, there is a way to disable this and use it outside of the container but it is *not*, and I cannot stress this more emphatically, *not* recommended.

          The other approach is to use a resource-ref in your web.xml/ejb-jar.xml file. This introduces a logical binding between a name and a raw jndi name. Consult the relevant J2EE specifications for more information.


          2,Is the database connections REALLY been pooled ,according the config like above ?


          Yes.


          3,how to config to let jboss server initiate serveral database connection when startup?


          JBoss 4.0.5 introduces a option to JBoss/JCA. The deault pool behavior does not construct managed connections on startup but rather, uses lazy initialization. The option allows the pool to prefill to the mininum allowance. Note, this can only be used for pools with the criteria of 'ByNothing'.

          See

          http://wiki.jboss.org/wiki/Wiki.jsp?page=ConfigDataSources

          and

          http://wiki.jboss.org/wiki/Wiki.jsp?page=JBossJCAPooling