Solved

connecting to weblogic with spring using XML datasource

Posted on 2011-03-08
6
1,229 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:Jtw549
  • 3
  • 3
6 Comments
 
LVL 35

Expert Comment

by:mccarl
ID: 35074451
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.
0
 

Author Comment

by:Jtw549
ID: 35083252
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.

0
 
LVL 35

Expert Comment

by:mccarl
ID: 35088048
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?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Jtw549
ID: 35096938
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.
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 35099934
Well, then yeah, you will have the setDataSource in the base class of your DAO class hierarchy. As for the xml, you would have the dataSource definition once, and then 6 (or 5, I couldn't quite work that one out from what you said) bean definitions where you inject the dataSource into each one. And yes, it is quite often the case that there are numerous ways of doing things in Spring. Try not to get to hung up on that, pick one way that works and just keep your code consistent, is the best advice I can give you there.
0
 

Author Closing Comment

by:Jtw549
ID: 35100307
Thanks for the help....I will be back if I have problems with my PreparedStatements :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now