Project

General

Profile

1
package eu.dnetlib.data.mapreduce.hbase.lodImport.utils;
2

    
3
import java.beans.PropertyVetoException;
4
import java.io.IOException;
5
import java.sql.SQLException;
6

    
7
import org.apache.log4j.Logger;
8

    
9
import com.jolbox.bonecp.BoneCP;
10
import com.jolbox.bonecp.BoneCPConfig;
11
import com.jolbox.bonecp.BoneCPDataSource;
12

    
13
public class DB  {
14
	 
15
	 private Logger log = Logger.getLogger(DB.class);
16

    
17
	 private BoneCPDataSource ds;
18
	 private BoneCP connectionPool;
19
	
20
	 public DB(){
21
		 
22
	 }
23
	 
24
//	 public DB(String conLine, String username, String password, String minCpart, String maxCpart, String part) throws IOException, SQLException, PropertyVetoException {
25
//	        try {
26
//	            // load the database driver (make sure this is in your classpath!)
27
//	            Class.forName("virtuoso.jdbc4.Driver");
28
//	        } catch (Exception e) {
29
//	        	log.error(e.getMessage(), e);
30
//	            return;
31
//	        }
32
//
33
//	        try {
34
//	        	
35
////	        	Will have to read Iterable via an external File (oozie Configuration maybe)
36
//	        	
37
//	            // setup the connection pool using BoneCP Configuration
38
//	            BoneCPConfig config = new BoneCPConfig();
39
//	            // jdbc url specific to the virtuoso, eg jdbc:mysql://127.0.0.1/yourdb
40
//	            config.setJdbcUrl(conLine);
41
//	            config.setUsername(username);
42
//	            config.setPassword(password);
43
//	            config.setMinConnectionsPerPartition(Integer.parseInt(minCpart));
44
//	            config.setMaxConnectionsPerPartition(Integer.parseInt(maxCpart));
45
//	            config.setPartitionCount(Integer.parseInt(part));
46
//	            // setup the connection pool
47
//	            connectionPool = new BoneCP(config);
48
//	        } catch (Exception e) {
49
//	        	log.error(e.getMessage(), e);
50
//	            return;
51
//	        }
52
//
53
//	    }
54
	 
55
	 public BoneCPDataSource getDatasource(String conLine, String username, String password, String minCpart, String maxCpart, String part) throws IOException, SQLException, PropertyVetoException {
56
		 try {
57
//	            // load the database driver (make sure this is in your classpath!)
58
	            Class.forName("virtuoso.jdbc4.Driver");
59
	        } catch (Exception e) {
60
	        	log.error(e.getMessage(), e);
61
	        }
62
		 
63
		 BoneCPConfig config = new BoneCPConfig();
64
         config.setJdbcUrl(conLine);
65
         config.setUsername(username);
66
         config.setPassword(password);
67
         config.setMinConnectionsPerPartition(Integer.parseInt(minCpart));
68
         config.setMaxConnectionsPerPartition(Integer.parseInt(maxCpart));
69
         config.setPartitionCount(Integer.parseInt(part));
70
         config.setPoolStrategy("CACHED");
71
		 
72
	  	ds = new BoneCPDataSource(config); 
73
        return ds;
74
	       
75
	    }
76
	 
77
	 
78
	 public  BoneCP getConnectionPool(String conLine, String username, String password, String minCpart, String maxCpart, String part) throws IOException, SQLException, PropertyVetoException {
79
		 try {
80
//	            // load the database driver (make sure this is in your classpath!)
81
	            Class.forName("virtuoso.jdbc4.Driver");
82
	        } catch (Exception e) {
83
	        	log.error(e.getMessage(), e);
84
	        }
85
		 
86
		 BoneCPConfig config = new BoneCPConfig();
87
         config.setJdbcUrl(conLine);
88
         config.setUsername(username);
89
         config.setPassword(password);
90
         config.setMinConnectionsPerPartition(Integer.parseInt(minCpart));
91
         config.setMaxConnectionsPerPartition(Integer.parseInt(maxCpart));
92
         config.setPartitionCount(Integer.parseInt(part));
93
        connectionPool = new BoneCP(config);
94
        
95
        return connectionPool;
96
	       
97
	    }
98
 
99
	 public void closeConnectionPool() throws SQLException {
100
	        connectionPool.shutdown();
101
	    }
102
	 
103
	 public void closeDatasource() throws SQLException {
104
	        ds.close();
105
	    }
106
	
107
}
(1-1/4)