While trying to connect to my Oracle 11.1.0.7.0 database using jdbc with the ojdbc6.jar of the matching version, I found two variants.
Assuming a string DBURL of the form
jdbc:oracle:thin:#//#DBSERV#:#DBPORT#/#DBSID#
where those hashed words ('...') are filled correct, they (the variants) look like
ods=new oracle.jdbc.pool.OracleDataSource();
ods.setPassword(Datenbankpasswort);
ods.setUser(Datenbankuser);
ods.setURL(DBURL);
dbconn=ods.getConnection;
java.sql.DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
dbconn=java.sql.DriverManager.getConnection(DBURL,Datenbankuser,Datenbankpasswort);
of course followed by dbconn.getConnection();.
So far, I think both should work - but I'm interested which way is better? Maybe one is deprecated?
Further I can only connect with variant 1. Maybe I missed something in variant 2.
I'm interested in any suggestions.
DataSource is the preferred way to provide connection to a database.
This kind of high-level object "do" the job for you and access to lower classes, like the DriverManager
You can find some implementations of DataSource more powerful, where connections are pooled and reusable: ConnectionPoolDataSource
DataSource are typically configured and registered into a JNDI tree.
Usually, application/web servers like Tomcat offer capability to configure (and share) a DataSource.
Related
Using spring/hibernate/C3P0 ComboPooledDataSource. I am on a legacy project with spring 2.5, hibernate 3.3 and newest C3P0.
I am using LocalSessionFactoryBean implementation.
I use the spring TransactionInterceptor to globally set the transaction attributes.
I am adding a second replicated database to be used for reporting queries only. This will be a read only database and I would like to set all the connections to be read only.
I was trying to create a second instance of LocalSessionFactoryBean that has data sources which reference secondary databases.
However, what I would like to do is set all these transactions to read only.
I was hoping there might be a way to do this in the ComboPooledDataSource datasource. For instance the apache commons BasicDataSource has a readOnly setting. The C3P0 one does not.
Next, I thought there might be a way to do it in the LocalSessionFactoryBean. But no luck finding that. Any ideas?
Create a database account that only has read-only access. That is the best way to guarantee that the user cannot write. Create a second data source for that account and have at it.
Trying to do this with Spring/Hibernate is not the proper way to do it. If you use the right tool for the job it will be much more clean, understandable and maintainable.
I have an LDAP-Server that contains a large set of user data and would like to import this into an Apache Solr index. The question is not about whether this is a good idea or not (as discussed here). I need this kind of architecture as one of our production systems depends on a Solr index of our ldap data.
I'm considering different options to do so, but I'm not sure which one should be preferred:
Option 1: Use the Apache Solr DataImportHandler:
This seems to be the most straight forward Solr way of doing so. Unfortunately there does not seem to be DataSource available that would work with LDAP.
I tried to combine the JdbcDataSource with the JDBC-LDAP-Bridge. In theory that might probably work but the driver looks quite dated (latest Version from 2007).
Another Option might be to write a custom LdapDataSource using some of the LDAP-Libraries for Java (probably Spring LDAP, directly via JNDI or something similar?).
Option 2: Build a custom Feeder:
Another option might be to write a standalone service/script that bridges between the two services. However that feels a bit like reinventing the wheel.
Option 3: Something I haven't thought of yet:
Maybe there are additional options here that I simply haven't discovered yet.
Solved it by writing a custom LDAP DataSource for the Solr DataImportHandler.
It's not as hard as it sounds. The JdbcDataSource can be used as a template for writing your custom DataSource, so basically you just have to rewrite that one Java-Class for the LDAP protocol.
For accessing the LDAP-Client there are numerous options, such as plain JNDI, UnboundID LDAP SDK, Apache LDAP API, OpenDJ LDAP SDK or OpenLDAP JLDAP (there are probably more but I only had a look at those).
I went for UnboundID LDAP due to its well documented API and full support for LDAPv3.
Afterwards it is just a matter of referencing the datasource from the data-config.xml.
A nice side-effect of this setup is, that you can use all the goodies that the Solr DataImportHandler provides while indexing the LDAP server (Entity Processors and Transformers). This makes it easy to map the data structure between LDAP and the Solr Index.
I want to open access to a database using JDBC protocol, so that many people in the company can access it.
The JDBC connection string would look like jdbc:sqlserver://[serverName[\instanceName][:portNumber].
I'm wondering if there is a way to have a layer of abstraction between the client and the server. Let me explain...
For example, using REST services, we can tell people to use a URL that looks like https://servername/path/to/resource/123, which we can url-rewrite to https://my-server/my-path/resource?id=123. Implementation, location, complexity is hidden from the user, and anything can be changed transparently.
Is there something similar we can do with JDBC ? For example, can I redirect jdbc://[serverName[\instanceName][:portNumber] to jdbc:sqlserver://[my--server[\my-instance][:my-port] ?
Thanks for your help !
We can't rewrite JDBC url.
But you can use JNDI (Javax Naming Directory Interface). You must configure JNDI data source first, ex: in tomcat 6.0
Then you can use this JNDI in your application
I am working on an app that uses SimpleJDBCTemplate as the wrapper to make JDBC calls.
However, instead of a conventional Datasource, I am choosing to use AbstractDataSource so I can choose from multiple data sources.
I am using ThreadLocal to inject keys to choose the appropriate Datasource.
However, it appears Spring is eagerly creating all my DAOs and my jdbcTemplate and hence I cannot figure out how to have the jdbcTemplate get Connection on demand.
Any clues.?
Do you mean AbstractRoutingDataSource? If not, you really should be using that, since this is exactly what it's for. Mark Fisher wrote a useful blog about it back when it was added to the framework.
Yes Spring will create your DAOs and JdbcTemplates eagerly if they're singletons, which is the default, but that doesn't mean they all obtain a connection immediately. A connection will only be obtained when you start some kind of operation that uses that data source. Typically, that would be starting a transaction. In other words, what you say you want to happen is what already happens.
I'm reading about c3p0 and dbcp for handling jdbc connections and heard alot of problems and people saying dbcp is dead but c3p0 can't do jdbc4 and so on. But I don't know if these posts are out of date.
Now I hit on BoneCP which explains how broken connections are handled here. Connections are wrapped and BoneCP does some prechecking of exceptions before they get passed to the application. If something is wrong with the connection BoneCP removes it from the pool.
1.) Do all these pools have that kind of connection handling?
2.) This question gets asked over and over again, but I couldn't find any answer from 2011. What one shall I use for a new application, that will be maintained for the next 10 years, if not more.
1.) I don't know about dbcp but with regards to C3P0 this functionality does exist in the C3P0PooledConnection class (look in the invoke method, the exception is caught and handled, if you want more details on the exact handling I can add them). I also needed to know if it contained it to drop testOnCheckin/Checkout and I verified it contains this behaviour.
2.) Really hard to say, since on the one hand C3P0 is widely used in many production sites and the maintainer has resumed active development but on the other hand BoneCP seems to have some very interesting design principles (pool sharding for examples) and some flattering benchmarks. Since you usually have a pretty good indirection from your connection pool library (mostly the dependency is contained to a config file or two) I can suggest to start with one and once you have a production with actual data try to optimize it and compare it with another library (also optimized of course). It very well may be that either library you choose will be sufficient for your needs.