| 1 |
|
package net.sf.sapjcosupport; |
| 2 |
|
|
| 3 |
|
import com.sap.mw.jco.IRepository; |
| 4 |
|
import com.sap.mw.jco.JCO; |
| 5 |
|
import org.apache.log4j.Logger; |
| 6 |
|
import org.springframework.dao.DataAccessResourceFailureException; |
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
0 |
public class SapDataSource { |
| 13 |
0 |
private static final Logger logger = Logger.getLogger(SapDataSource.class); |
| 14 |
|
|
| 15 |
|
private static JCO.Pool pool; |
| 16 |
|
private static IRepository repository; |
| 17 |
|
|
| 18 |
|
private String username; |
| 19 |
|
private String password; |
| 20 |
|
private String host; |
| 21 |
|
private String client; |
| 22 |
|
private String systemNr; |
| 23 |
0 |
private String language = "EN"; |
| 24 |
0 |
private int poolSize = 5; |
| 25 |
|
|
| 26 |
|
public String getUsername() { |
| 27 |
0 |
return username; |
| 28 |
|
} |
| 29 |
|
|
| 30 |
|
public void setUsername(String username) { |
| 31 |
0 |
this.username = username; |
| 32 |
0 |
} |
| 33 |
|
|
| 34 |
|
public String getPassword() { |
| 35 |
0 |
return password; |
| 36 |
|
} |
| 37 |
|
|
| 38 |
|
public void setPassword(String password) { |
| 39 |
0 |
this.password = password; |
| 40 |
0 |
} |
| 41 |
|
|
| 42 |
|
public String getHost() { |
| 43 |
0 |
return host; |
| 44 |
|
} |
| 45 |
|
|
| 46 |
|
public void setHost(String host) { |
| 47 |
0 |
this.host = host; |
| 48 |
0 |
} |
| 49 |
|
|
| 50 |
|
public String getClient() { |
| 51 |
0 |
return client; |
| 52 |
|
} |
| 53 |
|
|
| 54 |
|
public void setClient(String client) { |
| 55 |
0 |
this.client = client; |
| 56 |
0 |
} |
| 57 |
|
|
| 58 |
|
public String getSystemNr() { |
| 59 |
0 |
return systemNr; |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
public void setSystemNr(String systemNr) { |
| 63 |
0 |
this.systemNr = systemNr; |
| 64 |
0 |
} |
| 65 |
|
|
| 66 |
|
public String getLanguage() { |
| 67 |
0 |
return language; |
| 68 |
|
} |
| 69 |
|
|
| 70 |
|
public void setLanguage(String language) { |
| 71 |
0 |
this.language = language; |
| 72 |
0 |
} |
| 73 |
|
|
| 74 |
|
public int getPoolSize() { |
| 75 |
0 |
return poolSize; |
| 76 |
|
} |
| 77 |
|
|
| 78 |
|
public void setPoolSize(int poolSize) { |
| 79 |
0 |
this.poolSize = poolSize; |
| 80 |
0 |
} |
| 81 |
|
|
| 82 |
|
public IRepository getRepository() { |
| 83 |
0 |
getConnectionPool(); |
| 84 |
0 |
return repository; |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
public synchronized JCO.Client getConnection() throws DataAccessResourceFailureException { |
| 88 |
|
try { |
| 89 |
0 |
JCO.Pool pool = getConnectionPool(); |
| 90 |
0 |
return JCO.getClient(pool.getName()); |
| 91 |
0 |
} catch (Exception e) { |
| 92 |
0 |
throw new DataAccessResourceFailureException("Error creating connection to SAP", e); |
| 93 |
|
} |
| 94 |
|
} |
| 95 |
|
|
| 96 |
|
private synchronized JCO.Pool getConnectionPool() { |
| 97 |
0 |
if (pool == null) { |
| 98 |
0 |
String poolName = new StringBuffer("pool_").append(host).append(systemNr).append(client).toString(); |
| 99 |
0 |
pool = JCO.getClientPoolManager().getPool(poolName); |
| 100 |
0 |
if (pool == null) { |
| 101 |
0 |
logger.info(new StringBuffer("Creating new pool ").append(poolName).append(" , max size: ").append( |
| 102 |
|
poolSize).toString()); |
| 103 |
0 |
JCO.addClientPool(poolName, poolSize, |
| 104 |
|
client, username, password, language, host, systemNr); |
| 105 |
0 |
pool = JCO.getClientPoolManager().getPool(poolName); |
| 106 |
0 |
pool.setConnectionTimeout(60000); |
| 107 |
0 |
pool.setMaxWaitTime(60000); |
| 108 |
0 |
pool.setMaxConnections(poolSize * 2); |
| 109 |
|
} |
| 110 |
|
|
| 111 |
0 |
if (repository == null) { |
| 112 |
0 |
repository = JCO.createRepository("DefaultRepository", poolName); |
| 113 |
|
} |
| 114 |
|
} |
| 115 |
0 |
return pool; |
| 116 |
|
} |
| 117 |
|
|
| 118 |
|
public synchronized void release(JCO.Client connection) { |
| 119 |
0 |
if (connection != null) { |
| 120 |
|
try { |
| 121 |
0 |
JCO.releaseClient(connection); |
| 122 |
0 |
} catch (Exception e) { |
| 123 |
0 |
logger.warn("Error during release of connection...Trying to reset connection", e); |
| 124 |
|
try { |
| 125 |
0 |
connection.reset(); |
| 126 |
0 |
JCO.releaseClient(connection); |
| 127 |
0 |
} catch (Exception e1) { |
| 128 |
|
|
| 129 |
0 |
} |
| 130 |
0 |
} |
| 131 |
|
} |
| 132 |
0 |
} |
| 133 |
|
} |