Link to home
Start Free TrialLog in
Avatar of Jtw549
Jtw549

asked on

connecting to weblogic with spring using XML datasource

There was a topic on this subject earlier, that I wanted clarified a bit more for my code....I have it set up with a XML file. I'm doing this....

Connection class:
public class SpringJdbc2 extends SimpleJTemplate{
       private SimpleJdbcTemplate jdbcTemplate;
       private static DataSource ds=null;
             public static Connection getConnection()throws Exception{
             Connection cn = null;
             DataSource dataSource = null;
             //JdbcTemplate jdbc= new JdbcTemplate(dataSource);
             //DataSource ds = (DataSource)ctx.getBean("jndiTemplate");
             XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("Datasource.xml"));
             SimpleJTemplate myBean = (SimpleJTemplate) beanFactory.getBean("DMMDataSource2");      
             cn =ds.getConnection();

      return cn;
      }

SimpleJTemplate.java:
public class SimpleJTemplate {
        private SimpleJdbcTemplate simpleJdbcTemplate;

        public void setDataSource(DataSource dataSource) {
                this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
        }
        public void setDataSourceName(String name) throws NamingException {
            InitialContext ctx = new InitialContext();
            simpleJdbcTemplate = new  SimpleJdbcTemplate((DataSource) ctx.lookup(name));
        }
       
        }
Im guessing this would go into class that uses a connection:
 private SimpleJdbcTemplate simpleJdbcTemplate;

        public void setDataSource(DataSource dataSource) {
                this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
        }


Datasource.xml:
<bean id="DMMDataSource2" class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName" value="jdbc/DMMDataSource" />
      <property name="environment">
            <props>
                  <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
                  <prop key="java.naming.provider.url">http://localhost:7001</prop>
            </props>
      </property>
</bean><!-- *joe* -->

<bean id="myBean" class="path.to.MyBean">
    <property name="dataSource" ref="DMMDataSource2" />
  </bean>

of course MyBean will be any class that will use the datasource that connects to weblogic. I think im doing too much or not wrapping my head around the concept completely. Predominately, Im guessing I will then do a query method that I created either with jdbcTemplate.query or a  myBean.findEmp("Spring", 25);(example function called findEmp that takes 2 variables).
 
Please tell me if i'm screwing up on something.
Avatar of mccarl
mccarl
Flag of Australia image

Yeah, you are doing a bit too much. You don't need either of the first two classes (SpringJdbc2 or SpringJTemplate). Datasource.xml looks alright, and then all you need to do is code path/to/MyBean.java like this...

package path.to;

public class MyBean {
  private SimpleJdbcTemplate jdbcTemplate;
  public void setDataSource(DataSource dataSource) {
    jdbcTemplate = new SimpleJdbcTemplate(dataSource);
    }

  // Rest of your MyBean class where you call jdbcTemplate.query(), etc.
}


Basically, in words, Spring gets the DataSource from JNDI as DMMDataSource2 (in Datasource.xml) and injects that into your MyBean. As it is being set in MyBean, you create a SimpleJdbcTemplate from it (you could create any of the other Template or just set the datasource itself, but this is generally the way it is done). And then you use the template to query, etc.

Let me know if any of this doesn't make sense.
Avatar of Jtw549
Jtw549

ASKER

Ok thats a relief because I was pulling my hair out about how this was getting the information from the xml. So I put that code in every class and I should get a datasource...nice...my last question is a 2 part question. 1) the package you put up "package path.to;" is just the normal package that the class is in right? example: my class is already in "package com.maker.ground.manifest.dao;" or is it a special path I need to make to connect for datasource aka "package.to.MyBean"? 2)  The below code:

<bean id="myBean" class="path.to.MyBean">
    <property name="dataSource" ref="DMMDataSource2" />
  </bean>

Do I need to make one for every class that will use the datasource? Im guessing I wont need to....

These two questions are kind of hand and hand because Im not sure if the connection would be using  the xml part to get the datasource(i.e. reading the name of the bean and associating it  using the bean name or I can just make a generic name like MyBean OR associate the package "package path.to.MyBean" with Mybean in order to make the connection.) First time using Spring so I want to understand everything in case I have do it again.

1) Yeah, it is just your normal package. I just made it like that to fit the example that you gave. So if your package is as in the last post, the code would be...

package com.maker.ground.manifest.dao;

public class MyBean {
  private SimpleJdbcTemplate jdbcTemplate;
  public void setDataSource(DataSource dataSource) {
    jdbcTemplate = new SimpleJdbcTemplate(dataSource);
    }

  // Rest of your MyBean class where you call jdbcTemplate.query(), etc.
}


and the last part of your xml would be ....

<bean id="myBean" class="com.maker.ground.manifest.dao.MyBean">
    <property name="dataSource" ref="DMMDataSource2" />
  </bean>


Of course, substituting MyBean for your actual class name.


2) I didn't quite understand everything you were asking here, but the first part "Do I need to make one for every class that will use the datasource" then yes you do. Every class that actually calls method on either the dataSource object directly, or the jdbcTemplate that you create from the dataSource, needs to have the dataSource injected into it like we have done above.

However, it looks like you are using the DAO pattern, where you have one (or at least a small number) of classes that are responsible for making JDBC calls on the database. These are your DAO classes and are the only ones that need the dataSource injected. Then you have you other classes, that make use of the methods in your DAO classes, these need to have you DAO injected, but don't need to dataSource to be injected.

I think that you may be struggling (like I did when I was first learning Spring) with the concept of IoC that underlies Spring. To help you further there, I need to know what the general architecture of your application is? Is it a web app or just a standalone type application? And in high level, general terms, what is your application meant to do?
Avatar of Jtw549

ASKER

Well I have like 6 DAO classes, 5 of which do a connection to weblogic using a jndi connection, which in turn does a jdbc connection to the database. All of them doing prepared statements which I am currently trying to redo for spring (PAIN!). Got a log folder for logging some webapps jsp pages, propertie file, connection to a iseries server which I wont be touching because someone else is working on that, and a couple of servlets. We are basically getting information from the app and saving preferences or looking up data for printing. Most of the Daos are extending each other to run functions. So for all the DAOs now I have to remove the DBConnection pool and replace it with the spring connection. So Only I get the prepared statements created aka once I find out which way to make them, seems there are 2 ways in a jdbcTemplate (I switched to a jdbc Template now because simpleTemplate doesnt handle Preparedstatements), I should be able to test everything to see if it works.
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jtw549

ASKER

Thanks for the help....I will be back if I have problems with my PreparedStatements :)