7 Replies Latest reply on Mar 5, 2008 9:09 AM by marx3

    JDBC

    marx3

      Hello
      how to get JDBC Connection in Seam Component (or in EJB)?
      I don't need transaction, I need to only read some data

        • 1. Re: JDBC
          marx3

          Sorry for double post, forum has problems... :(

          • 2. Re: JDBC
            nickarls

            cast the entityManager.getDelegate() (Session in Hibernate case) and get the connection from there.


            or look it up in JNDI

            • 3. Re: JDBC
              tom_goring

              Or standard EJB3 injection


              E.g.


              @Stateless
              @Name("bankBalanceManager")
              public class BankBalanceManagerBean implements BankBalanceManager {
              ...
                   @Resource (mappedName=BLJndi.DataSourceName)
                   private DataSource jnetDS; 
              ...
              }
              


              • 4. Re: JDBC
                marx3

                In example above I had to know datasource name, I don't know it (it's changing)


                Is it what Niklas suggest?


                HibernateEntityManager emh = (HibernateEntityManager) em;
                Connection conn = emh.getSession().connection();



                Anyway it works, despite deprecated connection() method

                • 5. Re: JDBC
                  nickarls

                  Something like that, if it works, it ain't broken ;-) At least not yet

                  • 6. Re: JDBC

                    Hi!


                    I am curious... Why would you need to change the datasource name often?


                    AFAIK if you are creating your project with  eclipse, your datasource configuration should be stored in a file like nameofyourproject-ds.xml, its contents look like this:


                     
                    
                    <?xml version="1.0" encoding="UTF-8"?>
                    
                    <!DOCTYPE datasources
                        PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN"
                        "http://www.jboss.org/j2ee/dtd/jboss-ds_1_5.dtd">
                        
                    <datasources>
                       
                       <local-tx-datasource>
                          <jndi-name>nameofyourprojectDatasource</jndi-name>
                          <connection-url>jdbc:hsqldb:hsql://localhost/xdb</connection-url>
                          <driver-class>org.hsqldb.jdbcDriver</driver-class>
                          <user-name>sa</user-name>
                          <password></password>
                       </local-tx-datasource>
                        
                    </datasources>
                    
                    



                    In this case the name of you datasource is nameofyourprojectDatasource,and it shouldn't change... unless you manually change it (you can change the connection string and even the driver but keep the name without change).


                    Regards,
                    LuxSpes

                    • 7. Re: JDBC
                      marx3

                      It's simply - code I do is shared between a few projects. I feel datasource should be defined once in code