javax.naming.NameNotFoundException: Unable to find mail - java

I've setup a mail Resource definition in my context.xml
<Resource
name="MyMailSession"
auth="Container"
type="javax.mail.Session"
mail.smtp.sendpartial="true"
mail.debug="true"
mail.smtp.host="myhost.hosting.com"
/>
And a mail utility which looks up the mail session's JNDI:
public int send() throws Exception {
int msgCount = 0; //number of recipients
Context ctx = new InitialContext();
Session session = (Session) ctx.lookup("java:comp/env/mail/MyMailSession");
MimeMessage message = new MimeMessage(session);
But am getting the following exception:
javax.naming.NameNotFoundException: Name [mail/MyMailSession] is not bound in this Context. Unable to find [mail].
at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
...
Is the mail tag necessary as a prefix to the Resource name when it comes to mail or is it more of a convention? (i.e., looking up a User Transaction would be set as java:comp/UserTransaction)

Try with
<Resource
name="mail/MyMailSession"
auth="Container"
type="javax.mail.Session"
mail.smtp.sendpartial="true"
mail.debug="true"
mail.smtp.host="myhost.hosting.com"
/>
instead of
<Resource
name="MyMailSession"
auth="Container"
type="javax.mail.Session"
mail.smtp.sendpartial="true"
mail.debug="true"
mail.smtp.host="myhost.hosting.com"
/>
Edit : In that case it is necessary because your refer to the context as mail/MyMailSession in this line :
Session session = (Session) ctx.lookup("java:comp/env/mail/MyMailSession");
What follow java:comp/env/ must be your Resource name.

Related

Java error configuring jdbc

I have a web java project that when running some pages give me the following error:
Caused by: org.apache.ibatis.exceptions.PersistenceException:
Error building SqlSession.
The error may exist in SQL Mapper Configuration
Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.datasource.DataSourceException: There was an error configuring JndiDataSourceTransactionPool. Cause: javax.naming.NameNotFoundException: Intermediate context does not exist: jdbc/DashDBtest
I have the credentials of the database, but I do not know how to configure it to work
This error may related to DataSource configuration. if you use Apache Tomcat AS, it provide three ways to configure DataSource in JNDI.
1- in your application: context.xml under META-INF directory.
2- in your server context.xml under Tomcat/conf directory.
3- define it at global level by setting both server.xml and context.xml
I prefer the third way, so for Oracle Database declare the JNDI resource in your in your Tomcat/conf/server.xml like:
<GlobalNamingResources>
<Resource name="jdbc/DATABASE_NAME"
auth=Container
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
username="db_username"
password="db_password"
url="jdbc:oracle:thin:#//localhost:1521:XE"
/>
....
and for MySQL Database do:
<GlobalNamingResources>
<Resource name="jdbc/DATABASE_NAME"
auth=Container
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
username="db_username"
password="db_password"
url="jdbc:mysql://localhost:3306/DATABASE_NAME"
/>
....
And reference the JNDI resource link in your Tomcat/conf/context.xml :
<Context>
<ResourceLink name="jdbc/MY_DATABASE_NAME" global="jdbc/DATABASE_NAME" type="javax.sql.DataSource"/>
</Context>
and finally use the ResourceLink's name in your project to get the DataSource.
hope it help.

Embedded tomcat jndi definition

I'm using spring with embedded tomcat. I don't want application.properties to be used anymore, since it should be published to a webserver running tomcat and there I'm now using a JNDI dataSource from a context.xml which works very well. Now I want to define that JNDI resource for embedded tomcat, but it does not work.
What I've tried:
context.xml
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="jdbc/db" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/db"/>
</Context>
Configuration.java
#Bean
public DataSource dataSource() {
final JndiDataSourceLookup lookup = new JndiDataSourceLookup();
lookup.setResourceRef(true);
return lookup.getDataSource("jdbc/db");
}
#Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
return new TomcatEmbeddedServletContainerFactory() {
#Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {
tomcat.enableNaming();
return super.getTomcatEmbeddedServletContainer(tomcat);
}
};
}
and I'm getting:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]:
Factory method 'dataSource' threw exception; nested exception is org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'jdbc/db';
nested exception is javax.naming.NameNotFoundException: Name [jdbc/db] is not bound in this Context. Unable to find [jdbc].
Please help me, thanks :)
Try with
lookup.getDataSource("java:/comp/env/jdbc/db");
without java:comp, jndi can not be found."comp" is short for component and is bound to root context.

Spring / Tomcat - Error in DataSource (NameNotFoundException)

I am trying to connect a Spring MVC 4 application to MY Sql local BBDD.
This is the files:
Spring MVCConfiguration File:
#Bean
public DataSource dataSource() throws Exception {
Context cts = new InitialContext();
DataSource dts = (DataSource) cts.lookup("java:/comp/env/jdbc/etielaBBDD");
return dts;
}
Tomcat Context.xml:
<ResourceLink name="jdbc/etielaBBDD"
global="jdbc/BBDD"
auth="Container"
type="javax.sql.DataSource" />
Tomcat Server XML:
<Resource name="jdbc/BBDD" global="jdbc/BBDD" auth="Container" type="javax.sql.DataSource"
username="XXXXXX"
password="XXXXXX"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/confluenceuseUnicode=true&characterEncoding=utf8"
maxActive="15"
maxIdle="7"
defaultTransactionIsolation="READ_COMMITTED"
validationQuery="Select 1" />
And, when I Start Tomcat, this error appears:
javax.naming.NameNotFoundException: Name jdbc/BBDD is not bound in this context
I am two days trying to solve this error with no solution. Any idea? Thanks.

Constant java:comp not bound in this context error tomcat6

I keep getting this error and have tried a number of fixes rumoured online to no avail.
Here is the exception:
javax.naming.NameNotFoundException: Name java:comp is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at my.app.database.DatabaseManager.connect(DatabaseManager.java:44)
at my.app.database.DatabaseManager.return(DatabaseManager.java:133)
at my.app.GenParse.GeneratePastPositions.updateSatHistory(GeneratePastPositions.java:89)
at my.app.updateHistory.run(SatelliteEclipseServer.java:72)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:351)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
My resource is declared in context.xml as this:
<?xml version='1.0' encoding='utf-8'?>
<!-- The contents of this file will be loaded for each web application -->
<Context reloadable="true">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="myUsername" password="myPassword" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://mysql.myServer.com:3306/myUserName"/>
</Context>
And the connection in my java code:
public void connect() throws SQLException, NamingException{
initCtx = new InitialContext();
envCtx = (Context) initCtx.lookup("java:comp/env");
ds = (DataSource) envCtx.lookup("jdbc/TestDB");
}
I think also due to this its not closing the connection like it should. Other than the error message its all working, but I think this error compounds over time and uses up all the available connections.
I'd really appreciate some help with this as its been annoying me for weeks!

Extra Information in JNDI Definition

I'm developing a web application and it is to be deployed on Apache Tomcat 6.0. Application will be connecting a lot of databases (almost 25) so in order to manage the Connections, I'm using a context.xml file located under META-INF. So far so good, and here is what a Resource Definition looks like:
<Resource
name="jdbc/XX"
auth="Container"
type="javax.sql.DataSource"
username="XXX"
password="XXX"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:XXXX"
maxActive="8"
maxIdle="4"/>
Given this definition, a Connection object is created correctly.
What I'd like to know is if I am able to add extra information in this definition, such as projectName, and reach it from the context(or somewhere else). Something like the following:
<Resource
name="jdbc/XX"
auth="Container"
type="javax.sql.DataSource"
username="XXX"
password="XXX"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:XXXX"
maxActive="8"
maxIdle="4"
projectName="Cool Project"/>
Any help is appreciated..
No, but you could define a naming convention and add an Environment element for each of your resource :
<Environment name="XX_projectName"
value="Cool Project"
type="java.lang.String"
override="false"/>
In your code, you would access it via
Context ctx = new InitialContext();
String projectName = (String) ctx.lookup("java:comp/env/XX_projectName");
See http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Environment%20Entries for details.

Categories

Resources