21
1 Kaut kas priekš ievada 1 Ten to 15 years ago, if you were having performance issues with your database application, 95% of the time the issues were caused by your database management software. At that time, tuning the database was considered magic to everyone except for a select group of engineers and database experts, who most likely worked for the database vendors. They kept details about their databases a secret;it was proprietary. The performance landscape started to change when these experts began sharing their knowledge by writing books about database tuning and giving seminars for the public. Today, because of the volumes of database tuning information available through any bookstore or the Internet, years of experience, and vastly improved database monitoring tools, the task of tuning databases has become less painful. Other changes were taking place during these years, such as hardware costs decreasing and computing power increasing.Along with faster and less expensive hardware came the move from monolithic to networked environments and client/server computing (two- and three-tier environments). Today, most database applications communicate with databases over a network instead of communicating directly through interprocess communication on a single computer. With the shift to networked environments, software was needed to provide connectivity between the application and database that now were located on different computers.The database vendors were the first to provide this software as proprietary database middleware components for their databases,which added a new factor to the performance puzzle. Database middleware consists of all components that handle the application’s data request until that request is handed to the database management software. With the introduction of the networked environment, the database middleware layer included components such as these: • The network • Database client software such as Oracle Net8 • Libraries loaded into the application’s address space when it connects to a database such as SSL libraries for data encryption across the network Soon, the industry began to see a need for database connectivity standards, which would provide a common application programming interface (API) to access multiple databases. Without a common 1 John Goodson and Robert A. Steward. The Data Access Handbook Achieving Optimal Database Application Performance and Scalability. 2009.

Datu bāzes sistēmas Oracle11g JDBC draiveri (dziņi) Web viewType 3 drivers connect to an application server (such as WebLogic). ... use without an Oracle installation; can be used

  • Upload
    phamanh

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Datu bāzes sistēmas Oracle11g JDBC draiveri (dziņi) Web viewType 3 drivers connect to an application server (such as WebLogic). ... use without an Oracle installation; can be used

1

Kaut kas priekš ievada1

Ten to 15 years ago, if you were having performance issues with your database application, 95% of the time the issues were caused by your database management software. At that time, tuning the database was considered magic to everyone except for a select group of engineers and database experts, who most likely worked for the database vendors. They kept details about their databases a secret;it was proprietary. The performance landscape started to change when these experts began sharing their knowledge by writing books about database tuning and giving seminars for the public. Today, because of the volumes of database tuning information available through any bookstore or the Internet, years of experience, and vastly improved database monitoring tools, the task of tuning databases has become less painful. Other changes were taking place during these years, such as hardware costs decreasing and computing power increasing.Along with faster and less expensive hardware came the move from monolithic to networked environments and client/server computing (two- and three-tier environments). Today, most database applications communicate with databases over a network instead of communicating directly through interprocess communication on a single computer.With the shift to networked environments, software was needed to provide connectivity between the application and database that now were located on different computers.The database vendors were the first to provide this software as proprietary database middleware components for their databases,which added a new factor to the performance puzzle. Database middleware consists of all components that handle the application’s data request until that request is handed to the database management software.With the introduction of the networked environment, the database middleware layer included components such as these:• The network • Database client software such as Oracle Net8 • Libraries loaded into the application’s address space when it connects to a database such as SSL libraries for data encryption across the networkSoon, the industry began to see a need for database connectivity standards, which would provide a common application programming interface (API) to access multiple databases. Without a common API, the industry was faced with multiple database connectivity solutions;each database vendor provided its own proprietary API. For example, to develop an application that could access Microsoft SQL Server, Oracle, and IBM DB2, a developer would have to know three very different database APIs: Microsoft Database Library (DBLIB), Oracle Call Interface (OCI), and IBM Client Application Enabler (CAE), as shown in Figure 1-3.The advent of database connectivity standards,such as ODBC,solved this problem. With the introduction of database connectivity standards, database drivers were added to the database middleware layer.Among many other things,a database driver processes the standards-based API function calls, submits SQL requests to the database, and returns results to the application. Read Chapter 3, “Database Middleware: Why It’s Important,” for detailed information about what database drivers do and how your choice of drivers can affect the performance of your database applications.Today,even when the database is tuned well,we know that database applications don’t always perform as well as we would like. Where does that leave us? Where do we find the performance issues today? For the majority of database applications, the performance issues are now found in the database middleware. For most applications, 75% to 95% of the time it takes to process a data request is spent in the database middleware compared to 10 to 15 years ago, when the majority of the time was spent in the database management software. In most cases, performance issues seen in database applications are caused by the following:• The network

1 John Goodson and Robert A. Steward. The Data Access Handbook Achieving Optimal Database Application Performance and Scalability. 2009.

Page 2: Datu bāzes sistēmas Oracle11g JDBC draiveri (dziņi) Web viewType 3 drivers connect to an application server (such as WebLogic). ... use without an Oracle installation; can be used

2

• Database drivers• The environment • Poorly coded database applicationsThis book goes into detail about database middleware and database application performance issues.For now,let’s look at a few examples.The Network One of the most common performance issues of the network is the number of round trips required to complete an operation.The size of network packets contributes to how many round trips are needed.Network packets carry an application’s messages via the database middleware to the database and vice versa. The size of the packets makes a difference in the performance of your database application. The main concept to remember is that fewer packets sent between the application and the database equates to better performance; fewer packets mean fewer trips to and from the database. Think of it this way: Jim’s manager asks him to move five cases of diet soda from a second-floor office to the first-floor kitchen.If Jim’s packet size is a 6-pack rather than a case, he has to make 20 trips to the kitchen instead of five, which means he is taking longer to move the soda to the kitchen. We discuss more about networks, how they affect performance, and what you can do to optimize the performance of a network in Chapter 4, “The Environment:Tuning for Performance.”The Database Driver All database drivers are not created equal. The choice of which driver to use in database application deployments can largely impact performance. The following real-world scenario explains how one company solved its performance issues by changing only the database driver. DataBank serves the informational needs of both large and small companies through the retrieval, storage, and delivery of data. DataBank’s reputation and financial security depend on response time and system availability. It has contracts with its customers that require specific response times and system availability. If these requirements are not met, DataBank must pay its customers a fine. After a mandated upgrade to a new version of its database system and its accompanying middleware, DataBank began having serious performance issues. It was routinely paying more than $250,000 a month in fines due to missed contractual obligations.The situation was unacceptable; the company had to find the performance issues in its database application deployment. DataBank started by making sure that its database was optimally tuned. Even with the database performing well, the company was still missing its contractual service-level requirements. The system architect made a phone call to a database consultant, and the consultant asked,“Have you considered trying a different database driver?” The architect responded,“I didn’t even know that was an option.”The consultant recommended a database driver that he had used with success. Losingno time,the architect had the recommended database driver installed in a test environment.Within two days,the QA department reported a threefold improvement in average response time between the new and the currently deployed database drivers,as well as the elimination of stability problems. Based on the results of its performance testing,DataBank moved forward to purchase the new database driver. After the new database driver had been deployed for a couple of months,DataBank analyzed the revenue it was saving. DataBank was paying $250,000 in fines in September and reduced that to $25,000 by November.That is a savings of 90% in two months by simply changing the database driver. The new driver handled connection pooling and memory management more effectively than the old driver. DataBank solved several issues by deploying a new database driver: loss of revenue,dissatisfied customers,and overworked IT personnel,to name a few. Chapter 3 details the many ways that database drivers can affect performance and what you can do about it.

Page 3: Datu bāzes sistēmas Oracle11g JDBC draiveri (dziņi) Web viewType 3 drivers connect to an application server (such as WebLogic). ... use without an Oracle installation; can be used

3

Programmēšanas valoda Java un datu bāze

In addition to J2EE, Sun has published a specification for Java Data Objects (JDO)—an API for Java persistence. JDO provides a framework for mapping Java objects to a database of any kind and, therefore, isn't an object-relational API, per se. But naturally, implementations for relational databases, by definition, provide object-relational mapping. JDO provides nearly perfect transparent persistence. At the time of this writing—just six months since the release of the specification—there are at least nine commercial implementations already available and several open source projects underway.

Page 4: Datu bāzes sistēmas Oracle11g JDBC draiveri (dziņi) Web viewType 3 drivers connect to an application server (such as WebLogic). ... use without an Oracle installation; can be used

4

Page 5: Datu bāzes sistēmas Oracle11g JDBC draiveri (dziņi) Web viewType 3 drivers connect to an application server (such as WebLogic). ... use without an Oracle installation; can be used

5

Par draiveriem ko vajadzētu sakārtot un pielikt klāt2

JDBC savienojuma ar DB izveidošana

1. Import JDBC Packages: Add import statements to your Java program to import required classes in your Java code.

2. Register JDBC Driver: This step causes the JVM to load the desired driver implementation into memory so it can fulfill your JDBC requests.

3. Database URL Formulation: This is to create a properly formatted address that points to the database to which you wish to connect.

4. Create Connection Object: Finally, code a call to the DriverManager object's getConnection( ) method to establish actual database connection.

A data source can be a DBMS, a legacy file system, or some other source of data with a corresponding JDBC driver.

2 Mahmoud Parsian. JDBC Recipes. A Problem-Solution Approach. 2005.

Page 6: Datu bāzes sistēmas Oracle11g JDBC draiveri (dziņi) Web viewType 3 drivers connect to an application server (such as WebLogic). ... use without an Oracle installation; can be used

6

Datu bāzes draiveri jeb dziņi

A database driver is a software component that an application uses on demand to gain access to a database using a standards-defined method. Database drivers are a key component of your database application deployment, and they can affect the performance of your database application. You might be thinking that a driver is a driver is a driver.But that isn’t true. Here are two major reasons a database driver can degrade the performance of your database application:

1) The architecture of the driver is not optimal. 2) The driver is not tunable. It does not have runtime performance tuning options that allow

you to configure the driver for optimal performance.The type of options that we are talking about are ones that you can adjust to match your application and environment. For example, if your application retrieves large objects, look for a driver option that allows you to configure how much active memory the driver uses to cache a large object.

The standards-based API specifications define the required functionality that a database driver must implement to be compliant. All drivers are not created equal; some drivers on the market are implemented with only the required functionality, and some are implemented with much, much more. You may be surprised at how many tasks database drivers perform.The following list provides a brief description of the tasks. Remember that not all drivers implement all this functionality:1) Translate a standards-based API (such as ODBC or JDBC) into a low-level set of database API requests (such as to a proprietary database wire protocol) 2) Provide thread safety to the application when making database API requests 3) Provide a full state machine regarding environment context for making database connections and statements for executing SQL queries 4) Handle all data conversion from a proprietary database format of data into native language data types 5) Buffer database query results from the network into the application’s buffers 6) Manage the TCP/IP connection from the client to the database server 7) Provide load balancing to various database servers 8) Provide failover to backup database servers if fatal errors occur 9) Map errors from database-specific codes into standard errors 10) Provide data translation from client code pages to and from database-specific code pages 11) Take cross-database SQL and translate it to database-specific SQL12) Optimize database stored procedure requests from character-based SQL to database-specific RPC calls 13) Optimize data access to ensure scalability 14) Control the state of the network port to the database to ensure only one active statement is being processed 15) Emulate functionality not present in the database (for example, scrollable cursors,arrays of parameters,prepared statements) 16) Batch queries in chunks to get maximum throughput 17) Expose database options as configuration options 18) Manage database transaction states,including coordinating with distributed transaction coordinators 19) Provide connection pooling for the application 20) Provide network authentication and data encryption across the networkThe way all this functionality is implemented in a database driver is key to how well the driver performs.Even when two database drivers implement all the same functionality, the performance of the database drivers may be quite different when used with your database applications.If you are experiencing less than optimal performance with the database driver you are using,consider evaluating another database driver.

Page 7: Datu bāzes sistēmas Oracle11g JDBC draiveri (dziņi) Web viewType 3 drivers connect to an application server (such as WebLogic). ... use without an Oracle installation; can be used

7

JDBC interfeisa draiveri jeb dziņi

JDBC drivers implement the defined interfaces in the JDBC API for interacting with your database server. For example, using JDBC drivers enable you to open database connections and to interact with it by sending SQL or database commands then receiving results with Java.The Java.sql package that ships with JDK contains various classes with their behaviours defined and their actual implementaions are done in third-party drivers. Third party vendors implements the java.sql.Driver interface in their database driver.

Accordingto Sun Microsystems (http://java.sun.com/products/jdbc/driverdesc.html), JDBC technology drivers fit into one of four categories:

1) JDBC-ODBC bridge provides JDBC API access via one or more ODBC drivers. Note that some ODBC native code (and, in many cases, native database client code) must be loaded on each client machine that uses this type of driver. Hence, this kind of driver is generally most appropriate when automatically installing and downloading a Java technology application is not important.

2) a native-API and partly Java technology–enabled driver converts JDBC calls into calls on the client API for Oracle, Sybase, Informix, DB2, or other DBMSs. Note that, like the bridge driver, this style of driver requires that some binary code be loaded on each client machine.

3) a network-protocol fully Java technology–enabled driver translates JDBC API calls into a DBMS independent net protocol that is then translated to aDBMS protocol by aserver. This net server middleware is able to connect all its Java technology-based clients to many different databases. The specific protocol used depends on the vendor. In general, this is the most flexible JDBC API alternative. It is likely that all vendors of this solution will provide products suitable for intranet use. For these products to also support Internet access, they must handle the additional requirementsfor security, access through firewalls, and so on, that the Web imposes. Several vendors are adding JDBC technology–based drivers to their existing database middleware products.

4) a native-protocol fully Java technology–enabled driver converts JDBC technology calls into the network protocol used by DBMSs directly. This allows a direct call from the client machine to the DBMS server and is apractical solution for intranet access. Since many of these protocols are proprietary, the database vendors will be the primary source for this style of driver. Several database vendors have these in progress.

Page 8: Datu bāzes sistēmas Oracle11g JDBC draiveri (dziņi) Web viewType 3 drivers connect to an application server (such as WebLogic). ... use without an Oracle installation; can be used

8

1. tipa JDBC draiveris jeb dzinis

Driver translates its calls into ODBC calls. ODBC then interacts with the desired database. It is the most available but slowest type, and it works only on Microsoft Windows and Sun Solaris. There is only one driver in existence, sun.jdbc.odbc.JdbcOdbcDriver. You may use this driver to access the Microsoft Access personal database.In a Type 1 driver, a JDBC bridge is used to access ODBC drivers installed on each client machine. Using ODBC requires configuring on your system a Data Source Name (DSN) that represents the target database.When Java first came out, this was a useful driver because most databases only supported ODBC access but now this type of driver is recommended only for experimental use or when no other alternative is available.

Advantage The JDBC-ODBC Bridge allows access to almost any database, since the database's ODBC drivers are already available.

Disadvantages1. Since the Bridge driver is not written fully in Java, Type 1 drivers are not portable.2. A performance issue is seen as a JDBC call goes through the bridge to the ODBC driver, then to the database, and this applies even in the reverse process. They are the slowest of all driver types.3. The client system requires the ODBC Installation to use the driver.4. Not good for the Web.

Page 9: Datu bāzes sistēmas Oracle11g JDBC draiveri (dziņi) Web viewType 3 drivers connect to an application server (such as WebLogic). ... use without an Oracle installation; can be used

9

2. tipa JDBC draiveris jeb dzinis

Driver translates its calls to the native (C, C++, Pascal, and so on) API calls of the desired database, which then call the desired database. For example, Oracle Call Interface (OCI) calls are used in developing Oracle’s type 2 drivers. The Oracle OCI is aset of low level proprietary APIs used to interact with Oracle databases. It allows you to use operations such as logon, execute, parse, fetch, and so on. OCI programs are normally written in C or C++, although they can be written in almost any programming language; for details, see http://www.orafaq.com/ faqoci.htm. You will need a driver for each database and operating system combination. Performance is faster than type 4 drivers.These drivers typically provided by the database vendors and used in the same manner as the JDBC-ODBC Bridge, the vendor-specific driver must be installed on each client machine.If we change the Database we have to change the native API as it is specific to a database and they are mostly obsolete now but you may realize some speed increase with a Type 2 driver, because it eliminates ODBC's overhead.

Advantage1. Type 2 jdbc drivers are better performance than the JDBC-ODBC Bridge as the layers of communication (tiers) are less than that of Type1 and also it uses Native api which is Database specific.

Disadvantage1. Native API must be installed in the Client System and hence type 2 drivers cannot be used for the Internet.2. Like Type 1 drivers, it’s not written in Java Language which forms a portability issue. 3. If we change the Database we have to change the native api as it is specific to a database.

Page 10: Datu bāzes sistēmas Oracle11g JDBC draiveri (dziņi) Web viewType 3 drivers connect to an application server (such as WebLogic). ... use without an Oracle installation; can be used

10

3. tipa JDBC draiveris jeb dzinis

This driver is amultitier (n-tier) driver. It is database-independent unlike the type 1, 2, and 4 drivers. Type 3 drivers connect to an application server (such as WebLogic). The application server in turn connects to adatabase using atype 1, 2, or 4 driver. The user just specifies the type 3 driver’s class name and adata source name to connect to; there is no need to change the client code when the back-end database changes.In a Type 3 driver, a three-tier approach is used to accessing databases. The JDBC clients use standard network sockets to communicate with an middleware application server. The socket information is then translated by the middleware application server into the call format required by the DBMS, and forwarded to the database server.This kind of driver is extremely flexible, since it requires no code installed on the client and a single driver can actually provide access to multiple databases.You can think of the application server as a JDBC "proxy," meaning that it makes calls for the client application. As a result, you need some knowledge of the application server's configuration in order to effectively use this driver type.Your application server might use a Type 1, 2, or 4 driver to communicate with the database, understanding the nuances will prove helpful.

Advantage1. This driver is server-based, so there is no need for any vendor database library to be present on client machines.2. This driver is fully written in Java and hence Portable. It is suitable for the web.3. There are many opportunities to optimize portability, performance, and scalability. 4. The net protocol can be designed to make the client JDBC driver very small and fast to load.6. This driver is very flexible allows access to multiple databases using one driver.7. They are the most efficient amongst all driver types.

Disadvantage 1. It requires another server application to install and maintain. Traversing the recordset may take longer, since the data comes through the backend server.

Page 11: Datu bāzes sistēmas Oracle11g JDBC draiveri (dziņi) Web viewType 3 drivers connect to an application server (such as WebLogic). ... use without an Oracle installation; can be used

11

4. tipa JDBC draiveris jeb dzinis

The type 4 driver is for databases that have written their APIs in Java, not C or C++ . So, no translation is needed at runtime by the driver; it calls the database using its Java API. MySQL’s Connector/J driver and Oracle’s Thin driver are both type 4. Note that type 1, 2, and 4 are two-tier (client-server) drivers. In other words, there is no application server in the middle. You can use these drivers in typical client-server environments.In a Type 4 driver, a pure Java-based driver that communicates directly with vendor's database through socket connection. This is the highest performance driver available for the database and is usually provided by the vendor itself.This kind of driver is extremely flexible, you don't need to install special software on the client or server. Further, these drivers can be downloaded dynamically.

Advantage1. The major benefit of using a type 4 jdbc drivers are that they are completely written in Java to achieve platform independence and eliminate deployment administration issues. 2. It is most suitable for the web. 3. Number of translation layers is very less i.e. type 4 JDBC drivers don't have to translate database requests to ODBC or a native connectivity interface or to pass the request on to another server, performance is typically quite good. 3. You don’t need to install special software on the client or server. Further, these drivers can be downloaded dynamically.Disadvantage1. With type 4 drivers, the user needs a different driver for each database.

Page 12: Datu bāzes sistēmas Oracle11g JDBC draiveri (dziņi) Web viewType 3 drivers connect to an application server (such as WebLogic). ... use without an Oracle installation; can be used

JDBC thin driverJava sockets

JDBC OCI driverOCI C library

12

Datu bāzes sistēmas Oracle11g JDBC draiveri (dziņi)1. Plānais (thin) draiveris. It is a pure Java driver used on the client-side, without an Oracle client installation. It can be used with both applets and applications.

2. Oracle Call Interface (OCI) draiveris. It is used on the client-side with an Oracle client installation. It can be used only with applications.

3. Servera puses plānais (server-side thin) draiveris. It is functionally similar to the client-side thin driver. However, it is used for code that runs on the database server and needs to access another session either on the same server or on a remote server on any tier.

4. Servera puses iekšējais (server-side internal) draiveris. It is used for code that runs on the database server and accesses the same session. That is, the code runs and accesses data from a single Oracle session.

1. Thin driver, a 100% Java driver for client-side use without an Oracle installation; can be used with applets and applications. The Thin driver communicates from a client to an Oracle database through Java sockets, communicating with the SQL engine and PL/SQL engine in the database.

jdbc:oracle:thin:JAVA_L/janis@DATORS:1521:BAZE

2. OCI driver for client-side use with an Oracle client installation; can be used with applications. The OCI driver communicates from a client to the SQL and PL/SQL engines through the client-side OCI C library.

3. Server-side Thin driver, which is functionally the same as the client-side Thin driver, but is for code that runs inside an Oracle server and needs to access another session either on the same server or a remote server, including middle-tier scenarios. The server-side internal driver is located within an Oracle9i database and communicates directly to that database through the KPRB C library.

4. Server-side internal driver for code that runs inside an Oracle server and accesses the same session (that is, inside the Oracle session that it must access). The server-side Thin driver has similar functionality to the client-side Thin driver, being used to communicate from one Oracle server to another.

SQL enginePL/SQL engine

Server-side thin driver

JDBC server-side internal driver

KPRB C library

Oracle datu bāze

Java engineOracle datu bāzes

sistēma

Page 13: Datu bāzes sistēmas Oracle11g JDBC draiveri (dziņi) Web viewType 3 drivers connect to an application server (such as WebLogic). ... use without an Oracle installation; can be used

13

Datu bāzes sistēmas Oracle JDBC bibliotēka un JDBC draiveri

Divas vienāda nosaukuma klases

Page 14: Datu bāzes sistēmas Oracle11g JDBC draiveri (dziņi) Web viewType 3 drivers connect to an application server (such as WebLogic). ... use without an Oracle installation; can be used

14

Kuru JDBC draiveri lietot

If you are accessing one type of database, such as Oracle, Sybase, or IBM, the preferred driver type is 4.If your Java application is accessing multiple types of databases at the same time, type 3 is the preferred driver.Type 2 drivers are useful in situations where a type 3 or type 4 driver is not available yet for your database.The type 1 driver is not considered a deployment-level driver and is typically used for development and testing purposes only.

JDBC draiveru izvēles kritēriji

• Features and ease of use: Does it support the features you need? • Speed/performance: Does it handle the operations as fast as possible? • Reliability: Does it pass aset of standard tests? • Security: Does it handle privileges properly? • Portability: Can you use the driver in another platform?• Support: Can the selling company help you when you need some help? • Price: Can you afford the price with respect to your project requirements? • Open source: Will the license allow you to add/modify features? • Compatibility: Is the JDBC driver on the client side compatible with the one on the applicationserver?

1. In general, unless you need OCI-specific features, such as support for non-TCP/IP networks, use the Oracle JDBC Thin driver.2. If you want maximum portability and performance, then use the JDBC Thin driver. You can connect to Oracle Database from either an application or an applet using the JDBC Thin driver.3. If you want to use Lightweight Directory Access Protocol (LDAP) over Secure Sockets Layer (SSL), then use the JDBC Thin driver.4.If you are writing a client application for an Oracle client environment and need OCI-driver-specific features, such as support for non-TCP/IP networks, then use the JDBC OCI driver.5. If you are writing an applet, then you must use the JDBC Thin driver.6. For code that runs in the database server and needs to access a remote database or another session within the same database instance, use the JDBC server-side Thin driver.7. If your code runs inside the database server and needs to access data locally within the session, then use the JDBC server-side internal driver to access that server.